• Resolved creativ3y3

    (@creativ3y3)


    Hi I am having trouble trying to show empty categories in a dropdown. I can currently only get categories that have a post assigned too it. Im sure its simple, but havaing some issues. I’ve tries adding hide_empty, but no luck, so I’m probably doing it wrong, is anyone able to advise please? Code looks like below:

    $categories = get_categories(‘taxonomy=my_category’, array( ‘hide_empty’ => false, ‘parent’ => 0 ));

    $select = “<select name=’locations’ id=’cat’ class=’postform’>n”;
    $select.= “<option value=’-1′>Select category</option>n”;

    foreach($categories as $category){
    if($category->count > 0){
    $select.= “<option value='”.$category->name.”‘>”.$category->name.”</option>”;
    }
    }

    $select.= “</select>”;

    echo $select;

    Any help appreciated!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator t-p

    (@t-p)

    Have a read of this tutorial and see if it points you in the right direction:
    https://www.wpbeginner.com/wp-tutorials/how-to-show-empty-categories-in-wordpress/

    Thread Starter creativ3y3

    (@creativ3y3)

    Hi Thanks for getting back, Ive had a crack but tying myself a bit in knots with it I think, I tried this:

    add_filter( ‘my_category’, ‘wpb_force_empty_cats’ );
    function wpb_force_empty_cats($cat_args) {
    $cat_args[‘hide_empty’] = 0;
    return $cat_args;
    }

    and tried it both where I’m running my PHP script (below) and in the functions file but no luck – any ideas appreciated.

    php snippet:
    $categories = get_categories(‘taxonomy=my_category’);

    $select = “<select name=’locations’ id=’cat’ class=’postform’>n”;
    $select.= “<option value=’-1′>Select category</option>n”;

    foreach($categories as $category){
    if($category->count > 0){
    $select.= “<option value='”.$category->name.”‘>”.$category->name.”</option>”;
    }
    }

    $select.= “</select>”;

    echo $select;

    Hi @creativ3y3

    Try commenting out the add_filter statement and the wpb_force_empty_cats function.

    Then, change the first line of your PHP snippet …
    from

    $categories = get_categories(‘taxonomy=my_category’);

    to

    $categories = get_categories( array(
        'taxonomy' => 'my_category',
        'hide_empty' => false
    ) );

    EDIT: If I understood you correctly, you wanted to show categories that have no posts, so change these lines

    foreach($categories as $category){
      if($category->count > 0){
        $select.= “<option value='”.$category->name.”‘>”.$category->name.”</option>”;
      }
    }

    to

    foreach($categories as $category){
      $select.= “<option value='”.$category->name.”‘>”.$category->name.”</option>”;
    }
    • This reply was modified 6 years, 5 months ago by Gerry.
    Thread Starter creativ3y3

    (@creativ3y3)

    Hi @metamezzo, you’re a legend, thats exactly it! Thanks very much for pointing me in the right direction.

    Thread Starter creativ3y3

    (@creativ3y3)

    Marking as resolved

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘showing empty categories’ is closed to new replies.