【发布时间】:2018-03-14 18:05:28
【问题描述】:
我的循环如下所示:
<!-- loop for the posts here -->
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => 'news'
);
$query = new WP_Query($args);
while($query->have_posts()) : $query->the_post();
?>
<div class="news_box_content">
<h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
<figure><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></figure>
<?php if($post->post_excerpt) { ?>
<p><?php echo get_the_excerpt(); ?></p>
<a href="<?php the_permalink(); ?>">Read more...</a>
<?php } else {
the_content('Read More');
} ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
我使用了一个函数来计算摘录长度,但它不起作用。
function custom_excerpt_length(){
return 10;
}
add_filter('excerpt_length', 'custom_excerpt_length');
如何限制任何摘录的字符数?
【问题讨论】:
-
您需要在循环中调用自定义摘录函数,即 custom_excerpt_length();而不是 get_the_excerpt();
-
哎呀..是的,你是对的。
标签: wordpress