0

Ive seen similar questions on here but I am already using WP_Query and wp_reset_postdata.

In short I wrote a shortcode that queries our Custom Post Type to display posts specific to a category. Everything works except the shortcode is only displaying one post when I know there is more.

I know it is something super simple I missed :)

The Query:

function get_gallery_query( $category ){

    $gallery_query = new WP_Query(
        array(
            'post_type'=>'custom_gallery', 
            'posts_per_page'=> '100', 
            'tax_query' => array(
                array(
                    'taxonomy' => 'gallery_category',
                    'field' => 'slug',
                    'terms' => $category
                    )
                )
            )
        );

    if ( $gallery_query->have_posts() ) :

        //the loop
        while($gallery_query->have_posts() ) : $gallery_query->the_post();

        $output = '<article class="masonry-entry" id="post-' . get_the_ID() .'" ' . get_post_class() .' >
                <div class="masonry-thumbnail border">
                    <a href="' . get_post_permalink() . '" title="' . get_the_title() . '" class="inner-shadow">' . get_the_post_thumbnail(get_the_ID(), 'masonry-thumb') .'</a>
                </div><!--.masonry-thumbnail-->

            <div class="masonry-details">
                <h5><a href="' . get_post_permalink() . '" title="' . get_the_title() . '"><span class="masonry-post-title"> ' . get_the_title() .'</span></a></h5>
                <div class="masonry-post-excerpt">
                    ' . getPostLikeLink(get_the_ID()) .'  <p class="post-comment"><a href="' .get_post_permalink() . '"><i class="fa fa-comments qcomment" title="Comments"></i></a> ' . comments_number( ' ', '<sup>1</sup>', '<sup>%</sup>' ) . '</p>' . getBookmarkLink(get_the_ID()) . '
                </div><!--.masonry-post-excerpt-->
            </div><!--/.masonry-entry-details -->  
        </article><!--/.masonry-entry-->';

        endwhile; 

        wp_reset_postdata();

        else :
            $output = 'Sorry, but there are no galleries matching your request';

        endif;

        return $output;


}
6
  • Thanks, I was reviewing the WP_Query class as you typed that. I made the adjustment with the same issue; I updated the code to reflect what I have. Note: I am using this in a shortcode but did not include that code since I dont think that is the issue.
    – Derek
    Commented Dec 2, 2014 at 16:12
  • In cases like this, please post all relevant code. This will prevent others wasting their time pointing out errors that actually does not exists. Thank you Commented Dec 2, 2014 at 16:14
  • in each iteration of the loop, you overwrite the previous value of $output, you need to concatenate the previous value with the new value.
    – Milo
    Commented Dec 2, 2014 at 16:17
  • @Milo can you show me an example, Im not sure I follow
    – Derek
    Commented Dec 2, 2014 at 17:10
  • 1
    This is a small detail but maybe change 'posts_per_page'=> '100', to 'posts_per_page'=> 100, - so that it's passing a number instead of a string, though I'm not sure it matters.
    – Howdy_McGee
    Commented Dec 2, 2014 at 17:35

1 Answer 1

0

As @Milo said in his comment above, you need to use concatenation as this is a loop your in. So you need to add a value each iteration not replace it. Your $output = '' needs to become $output .= ''. Notice the point/dot before declaring the value, in PHP this is used for concatenation.

//the loop
        while($gallery_query->have_posts() ) : $gallery_query->the_post();

        $output .= '<article class="masonry-entry" id="post-' . get_the_ID() .'" ' . get_post_class() .' >
                <div class="masonry-thumbnail border">
                    <a href="' . get_post_permalink() . '" title="' . get_the_title() . '" class="inner-shadow">' . get_the_post_thumbnail(get_the_ID(), 'masonry-thumb') .'</a>
                </div><!--.masonry-thumbnail-->

            <div class="masonry-details">
                <h5><a href="' . get_post_permalink() . '" title="' . get_the_title() . '"><span class="masonry-post-title"> ' . get_the_title() .'</span></a></h5>
                <div class="masonry-post-excerpt">
                    ' . getPostLikeLink(get_the_ID()) .'  <p class="post-comment"><a href="' .get_post_permalink() . '"><i class="fa fa-comments qcomment" title="Comments"></i></a> ' . comments_number( ' ', '<sup>1</sup>', '<sup>%</sup>' ) . '</p>' . getBookmarkLink(get_the_ID()) . '
                </div><!--.masonry-post-excerpt-->
            </div><!--/.masonry-entry-details -->  
        </article><!--/.masonry-entry-->';

        endwhile;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.