我需要获取所有类别和类别ID。并使用以下代码显示:
echo '<li><a data-filter=".portfolio-'.$category->cat_ID.'" href="'.$cat_link.'">'
. $category->name.'</a></li>';
我在不同类别中有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;
}
}
答案 0 :(得分:0)
$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>';
}