0

I refer to this post where I have just taken my code from: How to display a listing template of a certain taxonomy?

I am trying to create a page template that will list all categories in "servicecats" taxonomy which is linked to the custom post-type "Services".

In the page template I have used the code taken from the link above:

<?php

        $taxonomy     = 'servicecats';
        $orderby      = 'name'; 
        $show_count   = 0;      // 1 for yes, 0 for no
        $pad_counts   = 0;      // 1 for yes, 0 for no
        $hierarchical = 1;      // 1 for yes, 0 for no
        $title        = '';

        $args = array(
          'taxonomy'     => $taxonomy,
          'orderby'      => $orderby,
          'show_count'   => $show_count,
          'pad_counts'   => $pad_counts,
          'hierarchical' => $hierarchical,
          'title_li'     => $title
        );

    ?>
<?php wp_list_categories( $args ); ?>

When I view the page, it just says "No categories" although I do have 4 set up, the taxonomy and post-type are both hierarchical.

Any suggestions? Thanks

3 Answers 3

2

I managed to sort this using:

<?php $category_ids = get_all_category_ids(); ?>
      <?php
      $args = array(
         'orderby' => 'slug',
         'parent' => 0,
        'taxonomy' => 'servicecats'
      );
      $categories = get_categories( $args );
      foreach ( $categories as $category ) {
         echo '<li><img src=""/><a href="' . get_category_link( $category->term_id ) . '" rel="bookmark">' . $category->name . '' . '' . $category->description . '</a></li>';
      }
     ?>

Would still like to know how to also show the empty categories though if anyone could offer advice on this?

3
  • You should accept your own answer. Please help keeping the site clean of unnecessary open questions :-) Commented Nov 20, 2014 at 18:43
  • Sorry Pieter, I did try to when I posted but told me I could only accept my own answer after x days and then forgot to come back.
    – Adrian
    Commented Nov 20, 2014 at 20:39
  • Why you create that $category_ids but never use it?
    – iceiceicy
    Commented Aug 13, 2023 at 7:13
1

Just use 'hide_empty' and set it to false as it's true per default.

wp_list_categories( array(
    'hide_empty' => false,
) );
0
1

The code in question works and I can only think of two reasons why it fails

  • Wrong taxonomy name supplied

  • The terms assigned to taxonomy is empty

I would suggest that you get wp_list_categories() working as it is faster and less resource intensive than get_categories()

To get empty terms, simply add the hide_empty parameter to your arguments and set it to 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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