只获得70%的帖子内容

时间:2017-07-18 00:50:17

标签: wordpress

我正在开发基于WP的RSS聚合网站,我的代码目前从外部网站获取所有内容。现在,我试图仅显示完整帖子内​​容的70%,以便我可以链接回原始内容。假设postID 1有350个单词,postID 2有600个单词,我希望内容分别为postID 1的245个单词和postID 2的420个单词(两者都应显示70%的可用内容)。以下代码的任何自定义版本应该适用于我:

<?php the_content(); ?>

1 个答案:

答案 0 :(得分:1)

在您的主题目录中找到functions.php,并为您的内容添加过滤器:

<?php   

add_filter("the_content", "plugin_strip");

function plugin_strip($content) {
    $length = strlen($content);
    $max_length = intval($length * 0.7);
    return substr($content, 0, $max_length);
}

或另一种方法:

<?php

$content = get_the_content();
$length = strlen($content);
echo substr($content, 0, intval($length * 0.7));
相关问题