0

I have categories and sub -categories with same name.. but I want to show only the categories which is on the top level and not in the sub categories

$qry=query_posts( array( 
    'post_status' => 'publish',
    'post_type' => 'custom_post',
    'author' => '12;,
    'showposts' => 9,
    'tax_query' => array(
    array(
        'taxonomy' => 'custom_cat,
        'terms' => 10,
        'field' => 'id'
    ) ),
    'orderby' => 'id',
    'order' => 'DESC'
));

my categories will be like

test
  --news
  --magzine
news
1

See Taxonomy Parameters documentation - there is include_children parameter, which you can set to false in your taxonomy query.

0

try this

while($qry->have_posts()){
 $qry->the_post;
 foreach((get_the_category()) as $category) {
   //check if category have parent or not
   if ($category->category_parent == 0) {
   //your code goes here for post
 }
}

important Link:

  1. get_the_category()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.