上一篇文章路途吧给大家分享了如何获取自定义文章类型下的分类法名称、 分类法链接以及分类法ID,这篇文章我们就来了解下WordPress如何获取自定义文章类型下的所有分类法,以及分类法中的所有文章循环。

WordPress获取自定义文章类型分类法的所有文章循环

用魏大佬的话来说就是 :

1.先调出所有分类咯
2.然后循环套循环
3.完事儿

这可能对WordPress大神来说确实不是什么难点,但是对于咱们这些WordPress初学者来说,还是需要点时间琢磨的。

经过一阵研究也终于初步研究出来,不过百度似乎也有类似的WordPress代码,下面贴出我写的代码,大神不要见笑。。

<?php
    $args=array(
        \'taxonomy\' => \'sitecat\',
        \'hide_empty\'=>\'0\',
        \'hierarchical\'=>1,
        \'parent\'=>\'0\',
    );
    $categories=get_categories($args);
    foreach($categories as $category){
        $cat_id = $category->term_id;
?>
//第一个循环为获取所有自定义文章类型的分类法。$cat_id

<?php
    $salong_posts = new WP_Query(
            array(
            \'post_type\' => \'site\',//自定义文章类型,这里为site
            \'ignore_sticky_posts\' => 1,//忽略置顶文章
            \'posts_per_page\' => 8,//显示的文章数量
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => \'sitecat\',//自定义文章类型的分类法名称
                    \'field\'    => \'id\',
                    \'terms\'    => $cat_id,//分类法ID
                )
            ),
        )
    );
    if ($salong_posts->have_posts()): while ($salong_posts->have_posts()): $salong_posts->the_post(); 
?>
//循环内容
<?php the_title(); ?>

<?php endwhile; endif; ?>
//第二个循环为获取所有分类法的文章循环

<?php }?>

更多技术教程,请关注技术教程网www.jishujc.com