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

WordPress优化学习 蓝风 2年前 (2021-11-19) 326次浏览 0个评论

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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  endwhileendif; wp_reset_query(); ?>
</ul>

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


文章内容来源于网络,版权归原作者所有,如有侵权请与我们联系,我们将及时删除。
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址