简体   繁体   English

通过发布获取所有类别-Wordpress

[英]Get all category with post - Wordpress

I need to get all category and category ID. 我需要获取所有类别和类别ID。 And use this code to display: 并使用此代码显示:

echo '<li><a data-filter=".portfolio-'.$category->cat_ID.'" href="'.$cat_link.'">'
  . $category->name.'</a></li>';

I have 3 post in different category, but my loop give me 6 category name (2x duplicate). 我在不同类别中有3个帖子,但是我的循环给了我6个类别名称(重复2次)。

<?php
$query = array(
    'post_type'      => 'post',
    'parent'         => '',
    'orderby'        => 'id',
    'order'          => 'DESC',
    'hide_empty'     => 1,
    'hierarchical'   => 1,
    'taxonomy'       => 'category'
);

$category_home = new WP_Query( $query );
if ( $category_home->have_posts() ) {
    while ( $category_home->have_posts() ) {
        $category_home->the_post();
        $categories = get_categories($args);
        foreach($categories as $category):
            $cat_link = get_category_link($category->cat_ID);
            $cat_name= $category->name; 
            echo '<li><a data-filter=".portfolio-'.$category->cat_ID.'" href="'.$cat_link.'">'.$category->name.'</a></li>';
        endforeach; 
    }
}
$args = array(
   'orderby' => 'id',
   'hide_empty'=> 0,
   'exclude'   => 1, //Child From Boxes Category 
);
$categories = get_categories($args);
foreach ($categories as $cat) {
   $category_link = get_category_link( $cat->cat_ID );
   echo '<li><a data-filter=".portfolio-'.$cat->cat_ID.'" href="'.$category_link.'">'.$cat->name.'</a></li>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM