其他分享
首页 > 其他分享> > wordpress调用文章摘要,若无摘要则自动截取文章内容字数做为摘要

wordpress调用文章摘要,若无摘要则自动截取文章内容字数做为摘要

作者:互联网

以下是调用指定分类文章列表的一个方法,作者如果有填写文章摘要则直接调用摘要;如果文章摘要忘记写了则自动截取文章内容字数做为摘要。这个方法也适用于调用description标签

<ul>
<?php
    $args=array(
        'cat' => 1,   // 分类ID
        'posts_per_page' => 10, // 显示篇数
    );
    query_posts($args);
    if(have_posts()) : while (have_posts()) : the_post();
?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> //标题
        <p>
        <?php if (has_excerpt()) {
                echo $description = get_the_excerpt(); //文章编辑中的摘要
            }else {
                echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170,"……"); 
//文章编辑中若无摘要,自动截取文章内容字数做为摘要,0表示开始的位置,170表示结束的位置
            } ?>
        </p>
    </li>
<?php  endwhile; endif; wp_reset_query(); ?>
</ul>

  这个方法是不是还不错,感兴趣的朋友可以去试试。

标签:调用,截取,摘要,posts,文章内容,wordpress,文章
来源: https://www.cnblogs.com/ytkah/p/11399935.html