jolen
jolen

Reputation: 191

Wordpress outputting tags in a loop

I'm trying to output all the tags associated with a custom post type via shortcode and it only appears to be bringing in 1 tag inside the $output.

outside the $output the code is fine.

code is:

function display_stores() {

    $args = array( 'post_type' => 'stores', 'posts_per_page' => 5 );

    $success = new WP_Query( $args );

        $output = '';

        while( $success->have_posts() ) {

            $success->the_post();

            $tags = get_the_tags($post_ID);

            foreach($tags as $tag) {

                return '<li>'. $tag->name . '</li>' ;


            }

            $output .= sprintf( "<div class='story-content left'>" );
            $output .= sprintf( "<h2>%s</h2>", get_the_title() );
            $output .= sprintf( '%s</div>', get_the_content() );
            $output .= sprintf( "Button");
            $output .= sprintf( "<div class='story-tags right'>" );
            $output .= sprintf( "<h4>Areas</h4><ul class='ul-arrows'>" );
            $output .= sprintf( $tags );
            $output .= sprintf( "</ul></div><hr>" );

        }

            wp_reset_query();

            return $output;

}

add_shortcode( 'display_stores', 'display_stores' );

Upvotes: 0

Views: 48

Answers (1)

Jim
Jim

Reputation: 22656

foreach($tags as $tag) {
    return '<li>'. $tag->name . '</li>' ;
}

The first time this is ran it will exit the function and return the li. I imagine you meant to add it to output.

$tagHTML = '';
foreach($tags as $tag) {
    $tagHTML .= '<li>'. $tag->name . '</li>' ;
}
//Later
$output .= $tagHTML;

Upvotes: 2

Related Questions