List of Directory categories in my template

Hi,

Trying to work out how to list the categories for my directory plugin within my sidebar.php page in my template.

I have this so far:
<?php if(get_post_type() == 'directory_listing') { ?>
<div class="menu-side">
<h2>Directory</h2>
<!--

    <?php some_fnc_here_to_get_categories(); ?>
-->
</div>
<?php } else { blah... ?>

Any ideas?

  • Kimberly
    • Champion of Loops

    Hi Ali,

    Welcome to the WPMUDEV Community!

    You can try this:

    <ul>
    
    <?php $categories = get_categories("taxonomy=yourtaxonomy");
    
    foreach ($categories as $category) :
    
    echo '<li>' . $category->name . '</li>';
    
    endforeach;
    
    ?>
    
    </ul>

    Please let me know if you have any luck with this.

    Best,

    Kimberly

  • ali_wright
    • WPMU DEV Initiate

    Hi Kimberly,

    Thanks for this :slight_smile:

    Got it to work. At first I was only seeing the category I was in (as I've only got one listing in one of the categories set up so far), then realised I needed to set an extra parameter 'hide_empty':

    <?php
    $listing_args = array('taxonomy'=>'listing_category','hide_empty'=>0);
    $categories = get_categories($listing_args);
    foreach ($categories as $category) :
       echo '<li><a href="../../listings-category/'.$category->slug.'/">' . $category->name . '</a></li>';
    endforeach;
    ?>

    Thanks again for your help!

    Ali.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.