1

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"]

1
  • And what's the output of this shortcode? Do you get an error message? Commented May 13, 2019 at 9:56

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.