Site logo

Archived topic

How to get CPT Categories to show up in the right sidebar of Archives?

5 replies · Started by Ema on January 12, 2022

Viewing posts 1–6 of 6

Hello GP Team!

I've created some CPTs for example 'reports', 'consultations' , 'briefings' ,
I'd like these to show up in the right side bar of the archive pages (which I can do via 'Elements' > 'Layout'

But, I need the CPT categories to show up first. Can only see native cats from posts.

I'm not sure how to get them to show up:

<?php $categories = get_terms( ['taxonomy' => 'reports'] ); ?>

<ul>
    <?php foreach($categories as $key => $category) { ?>
        <li>
            <a href="<?php echo esc_url( get_term_link($category) ); ?>">
                <?php echo esc_html($category->name); ?>
            </a>
        </li>
    <?php } ?>
</ul>

Thanks in advance
Ema

Hi there,

so is you function not working ? Or is just a case you want to add them somewhere else in the sidebar ? eg. as a widget.

Hi Ema,

We can turn it into a shortcode so you can run the snippet through a shortcode block placed on a specific part of the widget area.

add_shortcode( 'display_cpt_terms', function() {

    ob_start();
    // Start your PHP below
	
	$categories = get_terms( ['taxonomy' => 'reports'] ); 

    echo '<ul>';
    
    foreach($categories as $key => $category) {
        echo '<li><a href="'.esc_url( get_term_link($category) ).'">'.esc_html($category->name).'</a></li>';
    }
    
    echo '</ul>';
  
    // End your PHP above
    return ob_get_clean();
} );

With this, you can use the shortcode [display_cpt_terms].

Feel free to modify the PHP snippet to fit what you need it to do. :D

Thanks Elvin!
This is so helpful. Appreciate your time

:-)

No problem. Glad to be of any help. :D

This archived topic is closed to new replies.