I am trying to make a template for my custom taxonomies. I want to create a shortcode so that i can decide how many taxonomy I want to show and some other settings. But the code is not working.

function cat_name_list($atts) { 

    extract( shortcode_atts( array(
        'custom_taxonomy' => 'entertainment_category',
        'posts' => 5 ,
    ), $atts ) );

$terms = get_terms(
'taxonomy' => $custom_taxonomy,
'title_li' => '',
    'number' => $posts,
    'orderby' => $orderby,
    'order' => $order,


if ( $terms && !is_wp_error( $terms ) ) :
    <div class="category_holder">
        <?php foreach ( $terms as $term ) { ?>
        <?php  $image = get_field('category_image', $term->taxonomy.'_'.$term->term_id); ?>

         <a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
        <div class="category_boxs" style="background: url(' <?php echo $image['url']; ?>')">        
              <div class="hover_overlay"></div>
             <div class="cat_box_wrap">
             <div class="cat_box_title">
                 <?php echo $term->name; ?>

         <span class="pull-right"><i class="fa fa-chevron-right border border_white"></i></span>

                 <div class="clearfix"></div>

        <?php } ?>
<?php endif;


add_shortcode('category-thumb', 'cat_name_list'); 
add_filter('widget_text', 'do_shortcode');

What i am doing wrong?

  • You should avoid using extract, I notice that you check if the terms is not an error, but if it was a WP_Error object, it might have useful information, can you add an else statement and display that information? Also shortcodes return their output, they don't echo it – Tom J Nowell Jan 19 '20 at 1:19
  • what is the result of your code? generally, check the documentation about 'return'ing the result in a shortcode, instead of 'echo'ing it. and title_li is not an argument for get_terms() – Michael Jan 19 '20 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.