1

I'm trying to output the categories of my custom post type but instead of them displaying as li items, I woujld like them each to be a div so I can style them in nice floated boxes...

The code I have is as follows:

<?php
    $customPostTaxonomies = get_object_taxonomies('short_courses');

    if(count($customPostTaxonomies) > 0)
    {
         foreach($customPostTaxonomies as $tax)
         {
             $args = array(
                  'orderby' => 'name',
                  'show_count' => 0,
                  'pad_counts' => 0,
                  'hierarchical' => 1,
                  'taxonomy' => $tax,
                  'title_li' => '',
                  'hide_empty' => FALSE
                );

             $categories = wp_list_categories($args);
                if ( $categories ) {
                    printf( '<div class="sc-cat-items">%s</div>', $categories );
                }

         }
    }
?>

Having looked around its of forums, I keep seeing this same answer but this isn't working - its still displaying each category as an li item inside a ul...

My site is here: http://staging.seedcreativeacademy.co.uk/short-courses/

I want end up with he categories looking something like this: https://leicestercollege.ac.uk/courses/

  • Are you trying to get a list of all possible categories, or all categories for a given post? – Jacob Peattie Oct 23 '17 at 15:43
  • I'm trying to get a list of all categories inside the custom post type... the site is here: staging.seedcreativeacademy.co.uk/short-courses if it helps :) – Shaun Taylor Oct 23 '17 at 15:54
  • Well, I have the list - I just want to display them as divs so I can style them in boxes - like this here: leicestercollege.ac.uk/courses – Shaun Taylor Oct 23 '17 at 15:55
  • wp_list_categories is for generating a fully-formed html list of categories, if you want custom output, that's the wrong function. – Milo Oct 23 '17 at 15:58
  • Care to expand on that, Milo? What do I need to use instead? – Shaun Taylor Oct 23 '17 at 15:59
1

Ok, I found a way that works great for me here :)

<?php
    $customPostTaxonomies = get_object_taxonomies('short_courses');

    if(count($customPostTaxonomies) > 0)
    {
         foreach($customPostTaxonomies as $tax)
         {
             $args = array(
                  'orderby' => 'name',
                  'show_count' => 0,
                  'pad_counts' => 0,
                  'hierarchical' => 1,
                  'taxonomy' => $tax,
                  'title_li' => '',
                  'hide_empty' => FALSE
                  );

                $categories = get_categories( $args );
                foreach ( $categories as $category ) {
                    echo '<div class="sc-cat-items"><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a></div>';
                }

         }
    }
?>

If anyone would like to expand on why the original didn't work, please feel free...

| improve this answer | |

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.