开发者

How can i display post with 30 words only in wordpress

开发者 https://www.devze.com 2023-02-21 22:42 出处:网络
I am trying to show posts in my home page of my theme where i want to restrict the post content to be displayed as only 20 words. I can use get_content to get the content value but how can i restrict

I am trying to show posts in my home page of my theme where i want to restrict the post content to be displayed as only 20 words. I can use get_content to get the content value but how can i restrict it to show only 20 words. Is there something i can do in my theme开发者_如何学Python to have this restriction on displaying only in the home page

Thanks Prady


what you want to do is conditionally use excerpts. First you have to edit the excerpt length, Add this to your functions.php file:

function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');

If you want to change the text that it appends on the end, or remove it add this directly under the above:

function new_excerpt_more($more) {
global $post;
return ''; // this will remove it, '...' is pretty common to signify more content though.
}
add_filter('excerpt_more', 'new_excerpt_more');

Then, if your homepage is your blog page, add the following to index.php, otherwise add it to page.php or which ever template you're using for the page in question, instead of <?php the_content(); ?>:

<?php if (is_page('home')) { the_excerpt(); } else { the_content(); } ?>

This should do the trick and uses WordPress's built-it functionality.

I suggest trawling the WordPress codex, here, it's full of helpful goodies in regards to content manipulation.


Just use str_word_count function

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号