GET_POST仅显示5个帖子。(NOPAGING和POSTS_PER_PAGE-1不工作)

时间:2015-01-14 作者:Regalado Montoya

我有这段代码,但它只显示了5篇文章(使用自定义分类法),其中应该是7篇。如果我点击标记url,它会显示7个注册中心,所以问题就出在这段代码及其查询中。我已经尝试了nopage=true,posts\\u per\\u page=-1 numberposts=-1,并将最后两个设置为固定数字,如7或999;但这些选择似乎都没有奏效。

希望有人能帮助我。谢谢

add_shortcode(\'gpp_prod\', function( $atts, $content = null ){
ob_start();

$atts = shortcode_atts(
    array(
        \'category\' => \'0\'
    ), $atts);

extract($atts);

$args = array(
    \'post_type\'=>\'gpp_prod\',
    \'orderby\' => \'title\',
    \'order\' => \'ASC\'
);

if($category > 0 ){
    $args[\'tax_query\'] = array(
        array(
            \'posts_per_page\' => -1,
            \'taxonomy\' => \'cat_prod\',
            \'field\' => \'term_id\',
            \'nopaging\' => true,
            \'terms\' => $category
        )
    );
}

?>
<?php $products = get_posts($args);
$termt = get_term($category, \'cat_prod\');

<div class="prod-cats">
    <h1><?php echo $termt->name;?></h1>
    <?php if(count($products)>0) {
        foreach ($products as $key => $value) { ?>
        <div class="prod-group">
            <div class="prod-title"><?php echo do_shortcode( $value->post_title); ?></div>
            <div class="prod-low">
                <div class="prod-image"><img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($value->ID)); ?>"/></div>
                <div role="tabpanel" class="prod-info">
                    <!-- Nav tabs -->
                    <ul class="nav nav-tabs" role="tablist">
                        <li role="presentation" class="active"><a href="#tab-1-<?php echo $value->ID?>" aria-controls="home" role="tab" data-toggle="tab">Desc</a></li>
                        <li role="presentation"><a href="#tab-2-<?php echo $value->ID?>" aria-controls="profile" role="tab" data-toggle="tab">Specs</a></li>
                    </ul>

                    <!-- Tab panes -->
                    <div class="tab-content">
                        <div role="tabpanel" class="tab-pane active" id="tab-1-<?php echo $value->ID?>"><?php echo do_shortcode( $value->post_content); ?></div>
                        <div role="tabpanel" class="tab-pane" id="tab-2-<?php echo $value->ID?>"><?php $meta_value = get_post_meta( $value->ID, \'meta-text\', false );
                                               if( !empty( $meta_value ) ) {
                                                   echo $meta_value[0];
                                               } else {
                                                   echo \'No specs available\';
                                               }
                            ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php } ?>
    <?php
    }
    ?>
</div>
<?php
wp_reset_postdata();
return ob_get_clean();
});

2 个回复
SO网友:birgire

看起来您正在放置posts_per_page 参数位于tax_query 部分get_posts.

例如,您可以尝试:

$args = array(
    \'posts_per_page\' => -1,
    \'post_type\'      =>\'gpp_prod\',
    \'orderby\'        => \'title\',
    \'order\'          => \'ASC\'
);

if( $category > 0 ){
    $args[\'tax_query\'] = array(
        array(
            \'taxonomy\' => \'cat_prod\',
            \'field\'    => \'term_id\',
            \'terms\'    => $category
        )
    );
}
在代码片段中。

Update: 您还应避免使用extract(), 这被认为不是最好的PHP实践。它正在从WordPress核心中删除:例如检查this answerticket #22400.

SO网友:darrinb

这个\'posts_per_page\' => -1,\'nopaging\' => true, 不应该在你的tax_query 参数。这可能就是为什么要放弃它。

尝试:

$args = array(
    \'post_type\'=>\'gpp_prod\',
    \'orderby\' => \'title\',
    \'order\' => \'ASC\',
    \'posts_per_page\' => -1,
    \'nopaging\' => true,
);
以及:

$args[\'tax_query\'] = array(
    array(            
        \'taxonomy\' => \'cat_prod\',
        \'field\' => \'term_id\',
        \'terms\' => $category
    )
);

结束

相关推荐

为什么WP_QUERY对象中的QUERY_VARS会发生变化?

当用户的角色不同于时,我在查询WP\\U查询对象时遇到问题administrator我的WP\\U查询如下:function remove_new_c_post($author_id,$value_id) { $query = new WP_Query( array( \'post_type\' => \'custom_post_type\', \'author\' => $author_id,&