NEXT_POSTS_LINK在自定义循环中不起作用

时间:2018-04-17 作者:Jake Punton

我已经为帖子创建了一个自定义循环,它与引导程序一起工作,在其中,我添加了分页,但它似乎不起作用。它只显示一个空按钮。。。

<?php $args = array(
         \'post_type\' => \'post\',
         \'posts_per_page\' => 1
    );

    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
        $i = 0;
        while ($my_query->have_posts()) : $my_query->the_post();
            if($i % 2 == 0) { ?>
            <div class="row"><?php } ?>
                <div class="col-md-6">
                    <div class="overview">
                       <!-- Post details here -->
                    </div>
                </div>
                <?php $i++; if($i != 0 && $i % 2 == 0) { ?>
            </div><!--/.row-->
            <div class="clearfix"></div>
                <?php }
            endwhile; ?>
            <nav class="pagination">
                <button class="btn-whole"><?php previous_posts_link(\'Previous\') ?></button>
                <button class="btn-whole"><?php next_posts_link(\'Next\') ?></button>
            </nav>
    <?php } wp_reset_query(); ?>
你知道为什么会这样吗?

3 个回复
SO网友:NikHiL Gadhiya

只需使用echo和get_

<?php $args = array(
     \'post_type\' => \'post\',
     \'posts_per_page\' => 1
);

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
    $i = 0;
    while ($my_query->have_posts()) : $my_query->the_post();
        if($i % 2 == 0) { ?>
        <div class="row"><?php } ?>
            <div class="col-md-6">
                <div class="overview">
                   <!-- Post details here -->
                </div>
            </div>
            <?php $i++; if($i != 0 && $i % 2 == 0) { ?>
        </div><!--/.row-->
        <div class="clearfix"></div>
            <?php }
        endwhile; ?>
        <nav class="pagination">
            <button class="btn-whole"><?php echo get_previous_posts_link(\'Previous\'); ?></button>
            <button class="btn-whole"><?php echo get_next_posts_link(\'Next\'); ?></button>
        </nav>
<?php } wp_reset_query(); ?>

SO网友:Matt_Geo

我知道这篇帖子有点老了,但我也陷入了同样的困境,也许其他人会在这个问题上偶然发现答案。我想在文件中创建一个自定义循环index.php 我解决了使用documentation @Michael 在评论中发布。我只是把它转换成一个解决了的答案。

<?php
// set the "paged" parameter (use \'page\' if the query is on a static front page)
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;

$args_blog = array(
            \'post_type\' => \'post\',
            \'posts_per_page\' => 9,
            \'category__in\' => array( 2, 3 ),
            \'paged\' => $paged
          );

// the query
$the_query = new WP_Query( $args_blog ); 
?>

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

<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post(); 
?>
<?php the_title(); ?>
<?php endwhile; ?>

<?php

// next_posts_link() usage with max_num_pages
next_posts_link( \'Older Entries\', $the_query->max_num_pages );
previous_posts_link( \'Newer Entries\' );
?>

<?php 
// clean up after the query and pagination
wp_reset_postdata(); 
?>

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

SO网友:NikHiL Gadhiya
try now this code
<? 
    $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
   $args = array(
         \'post_type\' => \'post\',
         \'posts_per_page\' => 1,
         \'paged\' => $paged,
    );

    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
        $i = 0;
        while ($my_query->have_posts()) : $my_query->the_post();
            if($i % 2 == 0) { ?>
            <div class="row"><?php } ?>
                <div class="col-md-6">
                    <div class="overview">
                       <!-- Post details here -->
                    </div>
                </div>
                <?php $i++; if($i != 0 && $i % 2 == 0) { ?>
            </div><!--/.row-->
            <div class="clearfix"></div>
                <?php }
            endwhile; ?>
            <nav class="pagination">
                <button class="btn-whole"><?php previous_posts_link(\'prev\', $my_query->max_num_pages); ?></button>
                <button class="btn-whole"><?php next_posts_link(\'next\', $my_query->max_num_pages); ?></button>
            </nav>
    <?php } wp_reset_query(); ?>
结束

相关推荐

Loop inside query

我有一个数组来存储分类法和查询术语,但我不能使用foreach在tax\\u查询中循环我的数组。我拿到了505内线。 $query = array( \'post_type\' => $post_type, \'posts_per_page\' => -1, ); $query[\'tax_query\'] = array( \'relation\' => \'OR\