百度 | 神马 | 搜狗 | 技术文档 | 学习资料分享 - 记录帝国CMS及运维技术的点点滴滴
你的位置:首页 > 实用IT技术 » 正文

【Wordpress相关】WordPress 技巧:正确获取日志摘要

发布时间:2020-09-17

作品分类:Wordpress相关  摘要  函数  截取  获取  摘要  正确  技巧  日志

WordPress 技巧:正确获取日志摘要,

我们知道通过 WordPress 函数 get_the_excerpt() 可以获取日志的摘要,如果没有摘要,它会自动获取内容,并且截取。但是由于无法,这个函数总是无法截取好内容,显得摘要长短不一,所以我撰写了下面这个函数来解决这个问题。

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>

function get_post_excerpt($post, $excerpt_length=240){
    if(!$post) $post = get_post();

    $post_excerpt = $post->post_excerpt;
    if($post_excerpt == ''){
        $post_content = $post->post_content;
        $post_content = do_shortcode($post_content);
        $post_content = wp_strip_all_tags( $post_content );

        $post_excerpt = mb_strimwidth($post_content,0,$excerpt_length,'…','utf-8');
    }

    $post_excerpt = wp_strip_all_tags( $post_excerpt );
    $post_excerpt = trim( preg_replace( "/[\n\r\t ]+/", ' ', $post_excerpt ), ' ' );

    return $post_excerpt;
}

将上面的代码复制到当前主题的 functions.php,然后使用下面的方式调用:

<?php echo get_post_excerpt(); ?>

本站推荐使用的主机:,国外主机建议使用

Top