我有一个函数,可以将第一篇文章的摘录长度设置为与后面的文章不同的长度,但我无法让它正常工作。如有任何意见,将不胜感激。
functions.php (功能)
function custom_excerpt ($query) {
if ($query->current_post == 0)
$excerpt = wp_trim_words ($query->post->post_excerpt, 60);
else
$excerpt = wp_trim_words ($query->post->post_excerpt, 30);
return $excerpt;
}
add_filter( \'excerpt_length\', \'custom_excerpt\' );`
home.php (回路)<div class="section-content section-news-content">
<?php
$args = array(
\'posts_per_page\' => 5,
\'post_type\' => \'post\',
\'cat\'=>2,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
get_template_part( \'template-parts/content\', \'news\' );
}
wp_reset_postdata();
}
?>
</div>`
content-news.php (从循环中调用函数)<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="excerpts">
<div class="post-text">
<div class="entry-content">
<span class="entry-text">
<?php
custom_excerpt();
?>
</span>
</div><!-- .entry-content -->
</div>
</div>
</article>`
编辑:我的问题还没有完全得到回答,我刚刚意识到为什么。要澄清的是,这些摘录不是手动添加的,它们是\\u帖子的\\u摘录。我的问题的另一个内在原因是我有多个循环,每个循环可能需要不同的摘录长度,例如第一个循环,只有第一个摘录不同。第二个循环,第三个摘录是不同的。我对没有澄清表示歉意。