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

Home Forums Support [Resolved] How to get CPT Categories to show up in the right sidebar of Archives?

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

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2077869
    Ema

    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

    #2078175
    David
    Staff
    Customer Support

    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.

    #2078228
    Ema

    Hi David,

    Yes, I can’t seeem to find the correct function to allow me to show my CPT categories in the native WP categories.
    Like so https://www.dropbox.com/s/pf8ctkwavebhfof/Screenshot%202022-01-12%20at%2016.24.07.png?dl=0
    and yes to show them in the right sidebar widget.

    #2078615
    Elvin
    Staff
    Customer Support

    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. πŸ˜€

    #2079493
    Ema

    Thanks Elvin!
    This is so helpful. Appreciate your time

    πŸ™‚

    #2082679
    Elvin
    Staff
    Customer Support

    No problem. Glad to be of any help. πŸ˜€

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.