我正在尝试在我的主题的主页中显示帖子,我想限制帖子内容仅显示为20个单词。我可以使用get_content来获取内容值,但是如何限制它只显示20个单词。有什么我可以在我的主题做有只在主页上显示此限制我如何在wordpress中显示包含30个单词的帖子
感谢 Prady
我正在尝试在我的主题的主页中显示帖子,我想限制帖子内容仅显示为20个单词。我可以使用get_content来获取内容值,但是如何限制它只显示20个单词。有什么我可以在我的主题做有只在主页上显示此限制我如何在wordpress中显示包含30个单词的帖子
感谢 Prady
你想做的事是有条件使用摘录。如果你想改变它附加在文末,或将其删除直属上方添加此
function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');
:首先,你必须编辑摘录长度,添加到您的functions.php文件
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');
然后,如果您的主页是您的博客页面,添加以下的index.php,否则将它添加到page.php文件或曾经模板您正在使用的,而不是为<?php the_content(); ?>
有问题的页面,:
<?php if (is_page('home')) { the_excerpt(); } else { the_content(); } ?>
这应该做到这一点,并使用WordPress的内置功能。
我建议拖网WordPress代码,here,它在内容操作方面有很多有用的好处。
只需使用str_word_count功能
您可能还想看看the_excerpt:http://codex.wordpress.org/Function_Reference/the_excerpt – JohnP 2011-04-05 10:03:21
使用WordPress的源代码,也许你可以参考这个问题的wordpress文档 – nicola 2011-04-05 10:04:03