WordPress首页如何调用指定分类目录文章

时间:2021-11-08 07:16:30 投诉/举报

WordPress如何调用指定分类目录文章?其实这个问题很简单,通过以下这段代码即可实现。

使用方法:把以下代码添加到你希望显示的地方,并修改相关参数即可。

<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> //标题
        <span>
        <?php if (has_excerpt()) {
                echo $description = get_the_excerpt(); //文章编辑中的摘要
            }else {
                echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170,"……"); //文章编辑中若无摘要,自定截取文章内容字数做为摘要
            } ?>
        </span>
    </li>
<?php  endwhile; endif; wp_reset_query(); ?>
</ul>

如有不明白的,可以在文章下面留言。