WordPress使用默认分页的多个循环

时间:2018-04-19 作者:JudeAinsly

我试图将广告商分为三类(自定义分类法“ad\\U类别”),即基本类、休闲类和高级类。作为默认查询,一个页面包含10个结果,但它应该在顶部显示高级广告商,然后是临时广告商和基本广告商。

如果我总共有30个高级和休闲广告,基本广告从第4页开始。

我的代码是:

loop-suppliers-basic.php

<?php
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$title = get_term_by(\'slug\', get_query_var(\'term\'), get_query_var(\'taxonomy\') );
$args = array(
    \'post_type\' => \'suppliers\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'ad_category\',
            \'field\'    => \'slug\',
            \'terms\'    => array(\'basic_advertiser\'),
        ),
        array(
            \'taxonomy\' => \'supplier_categories\',
            \'field\'    => \'slug\',
            \'terms\'    => $title->slug,
        ),
    ),
    \'paged\' => $paged,
    \'orderby\' => \'rand\'
);

$the_query = new WP_Query( $args ); ?>

<?php if ($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post(); ?>

////////// Loop goes here /////////

<?php endwhile; endif; ?>

taxonomy-supplier_categories.php

<?php get_template_part(\'loop-suppliers-premium\'); ?>
<?php get_template_part(\'loop-suppliers-casual\'); ?>
<?php get_template_part(\'loop-suppliers-basic\'); ?>
<?php get_template_part(\'pagination\'); ?>

Pagination function

function wp_pagination() { global $wp_query; $big = 999999999; echo paginate_links(array( \'base\' => str_replace($big, \'%#%\', get_pagenum_link($big)), \'format\' => \'?paged=%#%\', \'current\' => max(1, get_query_var(\'paged\')), \'total\' => $wp_query->max_num_pages )); }

这确实按我想要的顺序显示,但分页出错。尝试使用**rewind\\u posts()等方法*但没有起作用。

1 个回复
SO网友:NikHiL Gadhiya

立即尝试此代码

    <?php
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$title = get_term_by(\'slug\', get_query_var(\'term\'), get_query_var(\'taxonomy\') );
$args = array(
    \'post_type\' => \'suppliers\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'ad_category\',
            \'field\'    => \'slug\',
            \'terms\'    => array(\'basic_advertiser\'),
        ),
        array(
            \'taxonomy\' => \'supplier_categories\',
            \'field\'    => \'slug\',
            \'terms\'    => $title->slug,
        ),
    ),
    \'paged\' => $paged,
    \'orderby\' => \'rand\'
);

$the_query = new WP_Query( $args ); ?>

<?php if ($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post(); ?>

////////// Loop goes here /////////

<?php endwhile; ?>

<nav class="pagination">
        <?php pagination_bar( $the_query ); ?>
</nav>
<?php wp_reset_postdata();

endif;



function wp_pagination($wp_query)
{
    $big = 999999999;
    echo paginate_links(array(
        \'base\' => str_replace($big, \'%#%\', get_pagenum_link($big)),
        \'format\' => \'?paged=%#%\',
        \'current\' => max(1, get_query_var(\'paged\')),
        \'total\' => $wp_query->max_num_pages
    ));
}

结束

相关推荐

Custom Pagination is Broken

下午好我正在为网站添加分页功能,目前工作正常,但由于某些原因,该功能会加载一个额外的页面。我们有7篇文章,我将其设置为每页显示4篇文章。然而,问题是分页加载了第三个不显示帖子的页面。我只是想知道为什么会这样,我怎么才能解决它?下面是我的意思的一个活生生的例子:http://weightcreative.com/blog http://weightcreative.com/blog/page/2http://weightcreative.com/blog/page/3以下是我的帖子页面中的代码:<?p