在WP查询快捷代码中对链接进行分页

时间:2017-05-05 作者:DEM

所以我一直在寻找这个答案,但没有找到答案——基本上我有一个包含在短代码中的WP查询。。我已经设置了paginate links函数,将其输出到页面上。但是,单击时页面url不会更改(href确实显示了正确的url--/page/2),我知道在短代码中执行时,这与主查询有关。任何帮助都将不胜感激。

$paged = ( get_query_var( \'paged\' ) ) ? absint( get_query_var( \'paged\' ) ) : 1;

$args = array(
    \'post_type\' => $type,
    \'posts_per_page\' => -1,
    \'orderby\' => \'rand\',
    \'paged\' => $paged

);

    $the_query = new WP_Query($args); 

// Put default query object in a temp variable
$tmp_query = $wp_query;
// Now wipe it out completely
$wp_query = null;
// Re-populate the global with our custom query
$wp_query = $the_query;

?>




    <div class="row">
<?php  if ( $the_query->have_posts() ) :  $i=0; ?>



    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); 
        $sphere = get_post_meta(get_the_id(),\'testimonial_profession\', true);
        if ($i == 2) {
        $i = 0;
        ?>
        </div>
        <div class="row">
        <?php
    }

    ?>
        <div class="col-md-6 col-sm-12 col-xs-12">
                        <div class="testimonial_inner_wrapper">
                                <?php if(!has_post_thumbnail()): ?>
                                <h4 class="testimonials-inner-title"><?php the_title(); ?></h4>
                                <?php if(!empty($sphere)): ?>
                                    <div class="testimonial_sphere"><?php echo($sphere); ?></div>
                                <?php endif; ?>
                                <div class="short_separator"></div>
                            <?php else: ?>
                                <div class="media">
                                    <div class="media-left media-top">
                                        <div class="testimonial-media-unit">
                                            <?php the_post_thumbnail(\'img-69-69\', array(\'class\'=>\'testimonial-media-unit-rounded\')); ?>
                                        </div>
                                    </div>
                                    <div class="media-body">
                                        <h4 class="testimonials-inner-title"><?php the_title(); ?></h4>
                                        <?php if(!empty($sphere)): ?>
                                            <div class="testimonial_sphere"><?php echo($sphere); ?></div>
                                        <?php endif; ?>
                                        <div class="short_separator"></div>
                                    </div>  
                                </div>
                            <?php endif; ?>
                                                        <div class="testimonial_inner_content" style="color:#aaaaaa"><p><?php the_excerpt(); ?></p>
</div>
                        </div> <!-- inner wrapper -->
                    </div>


    <?php $i++; endwhile; ?>
    <!-- end of the loop -->
</div><div class="poNav">
     <?php

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
    \'format\' => \'?paged=%#%\',
    \'current\' => max( 1, get_query_var(\'paged\') ),
    \'total\' => $the_query->max_num_pages
) );

    $wp_query = NULL;
$wp_query = $temp_query;
?></div>

    <?php wp_reset_postdata(); ?>
    <?php
    $myvariable = ob_get_clean();
    return $myvariable; ?>

<?php else : ?>
    <p><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></p>
<?php endif; 
}

1 个回复
SO网友:SopsoN

你的$args 变量将非常有用。

如我所见,您正在创建自定义WP\\U查询,因此get_query_var(\'paged\') 与您的$the_query = new WP_QUERY, 但是到global $wp_query.

可能的解决方案:声明global $paged 并将其添加到$args = array( /* your args here*/, \'paged\' => $paged );, 同时更改您的get_query_var(\'paged\')$paged

结束

相关推荐

Pagination - not progressing

好的,我添加了以下分页标记:<?php next_posts_link( \'Older posts\' ); ?> <?php previous_posts_link( \'Newer posts\' ); ?> 我可以在地址栏中看到查询字符串正在正确更改e、 g.localhost:8888/wordpress/?页码=3但是,页面结果保持不变(显示最初的前10个结果)。我这里出了什么问题?编辑:下面是代码(没有任何HTML):<!--Latest Po