• Resolved Animatedlead

    (@toluwanilydia)


    I have a custom post ‘downloads’ with two taxonomy ‘groups’ and ‘items’
    I want the terms from ‘groups’ and ‘items’ to span out in post linking to their archive pages using shortcodes.

    e.g

    Groups: Electronic
    Items: phone, Gaget

    What i want is the same as the ‘category’ and ‘tags’ on archive3d.net

    I’ve been able to get the following Codes

    /*******display tag in post********/
    function tags_in_post($atts) {    // [tags] outputs post's tags in a span
    global $post;
    $tags = '<span class="post-tags">';
    ob_start();
    the_tags( '<span class="post-tags">', ', ', '</span>' );
    $tags = ob_get_contents();
    ob_end_clean();
    return $tags;
    }
    add_shortcode ('tags', 'tags_in_post');

    This code works fine and spans out the terms but it only works with default post_tags

    /**
     * Example: [wp custom_taxonomy="taxonomy_slug"]
     */
    
    // First we create a function
    function list_terms_custom_taxonomy( $atts ) {
    // Inside the function we extract custom taxonomy parameter of our shortcode
        extract( shortcode_atts( array(
            'custom_taxonomy' => '',
        ), $atts ) );
    // arguments for function wp_list_categories
    $args = array(
    taxonomy => $custom_taxonomy,
    title_li => ''
    );
    // We wrap it in unordered list
    echo '<ul>';
    echo wp_list_categories($args);
    echo '</ul>';
    }
    // Add a shortcode that executes our function
    add_shortcode( 'ct_terms', 'list_terms_custom_taxonomy' );
    //Allow Text widgets to execute shortcodes
    add_filter('widget_text', 'do_shortcode');
    
    // shortcode to use [ct_terms custom_taxonomy=customtaxonomyname]

    Code Two is almost perfect my problems with this are;
    1. the list jumps to the top of the page. I want the terms in front of each taxonomy (where the shortcode is pasted).

    2. its a list, i want the terms spanned out in from of each other and separated by commas

    I’ve tried many plugins, tried to mix these two codes but I’ve not been able to achieve much.

    I’m very new to WordPress and Coding is quite new to me, I will be glad if somebody can spoon-feed me the code to achieve this. I’ve been stuck at this point for days.
    Please Help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The issue with jumping to the top is that for a shortcode, the code for the shortcode needs to return the output, not echo it.

    You can capture the output of a function and return it with the ob_start(); and ob_get_clean() functions. So a modified version of your code would be:

    <?php
    /**
     * Example: [wp custom_taxonomy="taxonomy_slug"]
     */
    
    // First we create a function
    function list_terms_custom_taxonomy( $atts ) {
        ob_start();
        // arguments for function wp_list_categories
        $args = array(
            taxonomy => $atts['custom_taxonomy'],
            title_li => ''
        );
        // We wrap it in unordered list
        echo '<ul>';
        echo wp_list_categories($args);
        echo '</ul>';
    
        return ob_get_clean();
    }
    // Add a shortcode that executes our function
    add_shortcode( 'ct_terms', 'list_terms_custom_taxonomy' );
    //Allow Text widgets to execute shortcodes
    add_filter('widget_text', 'do_shortcode');
    
    // shortcode to use [ct_terms custom_taxonomy=customtaxonomyname]

    I also removed your use of extract(). (https://make.www.ads-software.com/core/handbook/best-practices/coding-standards/php/#dont-extract)

    As for being a list, you probably want to use get_the_term_list() instead of wp_list_categories.

    Thread Starter Animatedlead

    (@toluwanilydia)

    Thank you JakePT for your fast response. It works.

    Thread Starter Animatedlead

    (@toluwanilydia)

    Can you please help me out with the second problem

    Its a list, I want the terms spanned out in from of each other and separated by commas

    e.g

    Groups: Electronic
    Items: phone, Gadget

    archive3d.net

    Thread Starter Animatedlead

    (@toluwanilydia)

    Thank you JakePT. Finally solved.

    /**
     * Example: [wp custom_taxonomy="taxonomy_slug"]
     */
    
    function list_terms_custom_taxonomy( $atts) {
        extract( shortcode_atts( array(
            'custom_taxonomy' => '',
        ), $atts ) );
    
        ob_start();
        global $post;
        $string1 .= get_the_term_list( $post->ID , $custom_taxonomy, ' ' , ', ' , ' ' );
        $string1 .= ob_get_clean();
        return $string1;
    }
    
    add_shortcode( 'ct_terms', 'list_terms_custom_taxonomy' );
    add_filter('widget_text', 'do_shortcode');
    
    // shortcode to use [ct_terms custom_taxonomy=customtaxonomyname]
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Displaying Taxonomy terms with Shortcode in Custom Post’ is closed to new replies.