I have followed the Wordpress docs and have created this custom function/shortcode, for some reason I cannot get it to work.

/** Get Stories Shortcode **/

    function register_shortcodes() {
        add_shortcode( 'stories', 'stories_func' );
    }
    add_action( 'init', 'register_shortcodes' );

    function stories_func( $atts ) {

        global $wp_query, $post;

        $atts = shortcode_atts( array(
            'cat' => ''
        ), $atts );

        $loop = new WP_Query( array(
            'posts_per_page'    => 4,
            'post_type'         => 'stories',
            'orderby'           => 'rand',
            'tax_query'         => array (
                array (
                    'taxonomy'  => 'story_category',
                    'field'     => 'slug',
                    'terms'     => array (sanitize_title($atts['cat']))
                )
            )
        ));

        if( ! $loop->have_posts() ) {
            return false;
        }

        while( $loop->have_posts() ) {
            $loop->the_post();
            echo the_title();
        }

        wp_reset_postdata();
    }

The shortcode I am using is: [stories cat="Career Stories"]

2 Answers

1
Nilambar On

Shortcode should always return value rather than echoing in the callback function. Please check following example. Shortcode output is collected using ob_get_contents() function and it is returned at the end.

function register_shortcodes() {
    add_shortcode( 'stories', 'stories_func' );
}
add_action( 'init', 'register_shortcodes' );

function stories_func( $atts ) {

    global $wp_query, $post;

    $atts = shortcode_atts( array(
        'cat' => ''
        ), $atts );


    $loop = new WP_Query( array(
        'posts_per_page'    => 4,
        'post_type'         => 'stories',
        'orderby'           => 'rand',
        'tax_query'         => array (
            array (
                'taxonomy'  => 'story_category',
                'field'     => 'slug',
                'terms'     => array (sanitize_title($atts['cat']))
                )
            )
        ));

    if( ! $loop->have_posts() ) {
        return false;
    }

    ob_start();

    while( $loop->have_posts() ) {
        $loop->the_post();
        the_title();
    }

    wp_reset_postdata();

    $output = ob_get_contents();
    ob_end_clean();
    return $output;
}
0
raju_eww On

Here is updated code.

function register_shortcodes() {
    add_shortcode( 'stories', 'stories_func' );
}
add_action( 'init', 'register_shortcodes' );

/**
 * Produtos Shortcode Callback
 * 
 * @param Array $atts
 *
 * @return string
 */
function stories_func( $atts ) {
    global $wp_query,
        $post;
   $terms = get_terms( array(
    'taxonomy' => 'story_category',
    'hide_empty' => false,
    ) );
   foreach($terms as $term)
   {
     $term_slugs[] = $term->slug;
   }
    $atts = $term_slugs;

    $loop = new WP_Query( array(
        'posts_per_page'    => 4,
        'post_type'         => 'stories',
        'orderby'           => 'rand',
        'tax_query'         => array( array(
            'taxonomy'  => 'story_category',
            'field'     => 'slug',
            'terms'     => $atts
        ) )
    ) );

    if( ! $loop->have_posts() ) {
        return false;
    }

    while( $loop->have_posts() ) {
        $loop->the_post();
        echo the_title();
    }

    wp_reset_postdata();
}

Tested and works well