【问题标题】:How can i display post with 30 words only in wordpress如何仅在 wordpress 中显示 30 字的帖子
【发布时间】:2011-04-05 10:00:47
【问题描述】:

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

谢谢 普拉迪

【问题讨论】:

标签: php wordpress


【解决方案1】:

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

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

如果你想更改它在末尾附加的文本,或者将其删除,请直接在上面添加:

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 函数

    【讨论】:

    • 谢谢...我读到了我们需要在这里正确设置优先级codex.wordpress.org/Plugin_API/Filter_Reference/…。你能告诉我我需要在哪里设置优先级吗?谢谢
    • 您需要在您的主题上使用此功能,在您显示帖子内容的地方(get_content 功能)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2023-03-10
    • 1970-01-01
    • 2019-07-18
    • 2016-07-26
    相关资源
    最近更新 更多