wordpress获取当前分类id调用多个子分类列表

我们在做wordpress网站的时候,常常需要调用多个子分类的文章列表,如果都是指定id 来进行分类文章调用,就会比较繁琐,也不利于后期更新。

今天给大家介绍一个获取多个分类列表的方法:

<?php
    global $cat;
    $cats = get_categories(array(
        'child_of' => 1,
        'parent' => $cat,
        'hide_empty' => 0
    ));
    $c = get_category($cat);
    if(empty($cats)){
?>
<div class=item>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php endwhile; ?>
    <?php else: ?>
    <?php endif; ?>
</div>

	<?php ?>

<?php
}else{
    foreach($cats as $the_cat){
        $posts = get_posts(array(
            'category' => $the_cat->cat_ID,
            'numberposts' => 10,
        ));
        if(!empty($posts)){
            echo '
            <div class=item2>';
                    foreach($posts as $post){
$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
                        echo '<div class=column5>
                  <div class=text9>
                    <div class=p10>
<a href='.get_permalink($post->ID).'><img src='.$full_image_url[0].'/></a></div>
<div class=text10>'.mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 150,...).'</div>
                    <div class=more4><a href='.get_permalink($post->ID).'>了解更多</a></div>
                  </div>
                </div>';
                    }
                echo '
            </div>';
        }
    }
}
?>	

通过这个代码,分类可以自动获取,也可以手动输入分类id,非常方便。上面部分是没有子分类,的文章列表调用,下面部分是调用分类列表文章。

相关文章

我想将wordpress的默认接口路由改掉,愿意是默认的带一个 wp...
wordpress自定义分类法之后,我看到链接都自动在后面添加了一...
事情是这样的,我用 get_post_type 函数创建了一个自定义分类...
最近网站莫名其妙的被顶上了,过一个多小时,就注册一个账号...
最近服务器要到期了,就想着把网站转移到另外一台服务器,本...
今天在写wordpress的接口,然后碰到个奇怪的问题,怎么访问都...