Support » Developing with WordPress » wp_category_list not working

  • Resolved karlazz

    (@karlazz)


    Hello,

    This code below works fine on another site that is running 3.4.2 and lists all the child categories. But on my new 3.5 site wp_list_categories does not output anything but No Categories. I checked to be sure that get_category_children finds categories for my test case. It does. So there should be categories to display.

    if (get_category_children($this_category->cat_ID) != "") {
        echo "<div class='ab-subcats' >";
    	echo ' <h3 class="title"></h3>';
        echo "<ul>";
        wp_list_categories('orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID);
        echo "</ul> ";
    	echo "</div>";
      }

    Any thoughts?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Upfront, the function get_category_children() is marked “depricated since WP 2.8”. Though even if it seems to be working you can’t rely on that.
    Assuming $this_category->cat_ID is containing a valid ID you could do this:

    $kitten = wp_list_categories(
      array(
        'orderby'             => 'id',
        'show_count'          => 0,
        'use_desc_for_title'  => 1,
        'child_of'            => $this_category->cat_ID,
        'echo'  	            => 0
      )
    );
    
    if( !preg_match( '#no categories#i', $kitten ) ) {
    
      echo "<div class='ab-subcats' >";
    	echo ' <h3 class="title"></h3>';
      echo "<ul>";
      echo $kitten;
      echo "</ul> ";
    	echo "</div>";
    
    }

    If that’s still not showing anything you could also add var_dump( $kitten ); and check (and maybe post) what wp_list_categories() is returning.

    Thread Starter karlazz

    (@karlazz)

    Hi,

    Thanks for the reply. wp_list_categories is returning “No Categories”, even though there are children categories for it to display. Even without the get_category_children() call, I know that my test case has child categories.

    Thanks again.

    So the next variable to check would be var_dump( $this_category->cat_ID ); Does it show a proper ID?
    You can cross check manually in the admin backend under posts/categories and hover over the category names. The links would contain something like &tag_ID={category_id}

    If $this_category->cat_ID does show an existing ID you could also use get_categories() as an alternative to wp_list_categories which then will/should return an array instead of an HTML formatted list.

    $kitten = get_categories(
      array(
        'type'      => 'post',
        'child_of'  => $this_category->cat_ID,
        'orderby'   => 'id',
        'order'     => 'ASC',
        'taxonomy'  => 'category'
      )
    );

    Other than that I’m out of ideas (for now), sry.

    Thread Starter karlazz

    (@karlazz)

    Thanks, I will double check. I had a second site with the same code running and I just switched it to 3.5 and it is working fine, so there must be something else. Glad it’s something else. Thank you for your observations and I will keep in mind the deprecated code.

    The function wp_list_categories is declared in wp-includes\category-template.php You could also copy the old function from your 3.4.2 installation, rename it to, say, wp_list_categories_old, then call this instead of wp_categories in your code and see if that’s returning the proper list.

    Thread Starter karlazz

    (@karlazz)

    Thank you so much for your attention on this Chris! I like your last suggestion and will try it out. That’s a clever idea.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘wp_category_list not working’ is closed to new replies.