我正在处理一个搜索页面,我想从该页面或帖子内容中返回只有几行的数据。 我可以用PHP做到这一点,但我正在寻找WordPress方面的解决方案。 我搜索了它,但没有令人满意的结果。
下面是显示内容的代码
<div class="testimonial-content">
<div class="thumb search_page_individual_contents">
<?php the_content();?>
<a href="<?php the_permalink(); ?>">
</a>
</div>
</div>
因为 the_content() 如果有人喜欢,将显示所有内容,但我希望它只显示该页面或帖子的前三行。
您能否在显示 serach 结果的地方使用此代码:
<?php the_excerpt(); ?>
并在函数中添加这行代码.php
function new_excerpt_more($more) {
global $post;
return '... <a href="'. get_permalink($post->ID) . '">Read more</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
你也可以
试试wp_trim_words()
<?php
echo wp_trim_words(the_content(), 40,
'<a href="'. get_permalink() .'"> ...Read More</a>');
?>