首页 文章

只获得70%的帖子内容

提问于
浏览
0

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

<?php the_content(); ?>

1 回答

  • 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));
    

相关问题