如何在wordpress中仅显示30个字的帖子 [英] How can i display post with 30 words only in wordpress

查看:18
本文介绍了如何在wordpress中仅显示30个字的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的主题主页中显示帖子,我想将帖子内容限制为仅显示 20 个字.我可以使用 get_content 来获取内容值,但如何限制它只显示 20 个单词.我可以在我的主题中做些什么来限制仅在主页中显示

谢谢普拉迪

解决方案

你想做的是有条件地使用摘录.首先,您必须编辑摘录长度,将其添加到您的functions.php 文件中:

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

如果你想改变它附加在最后的文本,或者删除它,直接在上面添加:

function new_excerpt_more($more) {全球 $post;返回 '​​';//这将删除它,尽管 '...' 表示更多内容很常见.}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 的内置功能.<​​/p>

我建议在 此处 搜索 WordPress 代码,它充满了有关内容操作的有用的好东西.

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 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.

这篇关于如何在wordpress中仅显示30个字的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆