2011-04-05 43 views
1

我正在尝试在我的主题的主页中显示帖子,我想限制帖子内容仅显示为20个单词。我可以使用get_content来获取内容值,但是如何限制它只显示20个单词。有什么我可以在我的主题做有只在主页上显示此限制我如何在wordpress中显示包含30个单词的帖子

感谢 Prady

+1

您可能还想看看the_excerpt:http://codex.wordpress.org/Function_Reference/the_excerpt – JohnP 2011-04-05 10:03:21

+0

使用WordPress的源代码,也许你可以参考这个问题的wordpress文档 – nicola 2011-04-05 10:04:03

回答

3

你想做的事是有条件使用摘录。如果你想改变它附加在文末,或将其删除直属上方添加此

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,它在内容操作方面有很多有用的好处。

2

只需使用str_word_count功能

+0

指向英文版的链接该网站 – JohnP 2011-04-05 10:07:59

+0

谢谢...我读到那里我们需要在这里正确设置优先级http://codex.wordpress.org/Plugin_API/Filter_Reference/excerpt_length#Examples。你能让我知道我需要设置优先级吗?谢谢 – Prady 2011-04-05 10:21:42

+0

您需要在您的主题上使用此功能,以显示您的帖子内容(get_content函数) – inlanger 2011-04-05 10:27:38

相关问题