短码显示CPT查询只显示1个POST?

时间:2014-12-02 作者:Derek

我在这里看到了类似的问题,但我已经在使用WP\\u Query和WP\\u reset\\u postdata。

简而言之,我编写了一个快捷代码,用于查询我们的自定义帖子类型,以显示特定于某个类别的帖子。当我知道还有更多的帖子时,除了短代码外,其他都正常。

我知道我错过了一件非常简单的事情:)

查询:

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;


}

1 个回复
SO网友:Matt Royal

正如@Milo在上面的评论中所说,您需要使用串联,因为这是一个循环。因此,您需要在每次迭代中添加一个值,而不是替换它。你的$output = \'\' 需要成为$output .= \'\'. 在声明值之前请注意点/点,在PHP中,这用于串联。

//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;

结束

相关推荐

OOP and WordPress shortcode

我试图通过这种方式添加一个短代码class MyPlugin { function __construct() { $this->make_shortcode(); } function ShowMsg($cls, $lst4) { $data = shortcode_atts(array(\'phn\' => \'\', \'msg\' => \'\'), $atts);