widgets – Use atributes of shortcode in get template part

Question

Hi guys im doing a custom shortcode where the client can set a custom taxonomy and display in my sidebar

I want to use a template using the get_template_part but i dont get the terms of the taxonomy

I’m using the following code to display the shortcode

function cat_widgets( $atts ) {
    ob_start();
    get_template_part('templates/show-taxonomy');
    return ob_get_clean();
} 
add_shortcode( 'cat_widgets', 'cat_widgets' );

add_filter( 'widget_text', 'do_shortcode' );

And in the show-taxonomy.php my code is

<?php
    $atts = shortcode_atts( array(
        'custom_taxonomy' => '',
    ), $atts, 'atributes' );
    $terms_categorias = get_terms([
        'taxonomy' => $atts['custom_taxonomy'],
        'hide_empty' => false,
        'orderby' => 'term_id',
        'order' => 'ASC' 
    ]);
    ?>
    
    <ul class="product-categories">
        <?php foreach($terms_categorias as $term): ?>
            <li class="cat-item">
                <a href="<?php echo esc_url( get_term_link($term) ); ?>">
                    <?php echo esc_html($term->name); ?>
                </a>
            </li>
        <?php endforeach; ?>
    </ul>
    

I also tried to put the atts and get the terms in the shortcode after de ob_start(); but the returns is the same

Is there something that im missing?

0
Maria 3 weeks 2021-09-01T20:41:49-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse