不显示分页自定义帖子、旧按钮和新按钮

时间:2016-10-06 作者:moneycode

我正在尝试将我的自定义帖子分页,一旦达到每页帖子的限制,就可以有多个页面。最终目标是让url有/页=2等。我已经将测试限制为每页一篇文章(目前有两篇文章),但底部的新旧按钮没有显示出来?我做错了什么?

    <div class="col-md-8">

    <!-- set up or arguments for our custom query -->
    <?php
    $paged = ( get_query_var(\'page\') ) ? get_query_var(\'page\') : 1;
    $query_args = array(
        \'post_type\' => \'news\',
        \'posts_per_page\' => 1,
        \'paged\' => $paged
    ); ?>

    <?php $news = new WP_Query(array(
            \'post_type\' => \'news\'
        ));  ?>
    <?php while($news->have_posts()) : $news->the_post(); ?>
        <h2>
            <a href="<?php the_permalink(); ?>"><?php the_title();?></a>
        </h2>
        <p class="lead">
            <?php $author = get_the_author(); ?>
            by <?php echo $author ?>
        </p>
        <p><i class="fa fa-clock-o"></i> Posted on <?php the_time(\'F j, Y\'); ?></p>
        <hr>
        <a href="#">
            <?php if( get_field(\'image\') ): ?>
            <img class="img-responsive img-hover" src="<?php the_field(\'image\'); ?>" alt="">
            <?php endif; ?>
        </a>
        <hr>

        <?php if(get_field(\'news_content\')): ?>
            <p class="description"><?php echo get_field(\'news_content\'); ?></p>
        <?php endif; ?>

        <a class="btn btn-primary" href="<?php the_permalink(); ?>">Read More <i class="fa fa-angle-right"></i></a>

        <hr>


    <?php endwhile ?>

        <!-- Pager -->
        <?php if ($news->max_num_pages > 1) { ?>
        <ul class="pager">
            <li class="previous">
                <a href="<?php echo get_next_posts_link( \'Older Entries\', $the_query->max_num_pages ); ?>">&larr; Older</a>
            </li>
            <li class="next">
                <a href="<?php echo get_previous_posts_link( \'Newer Entries\' ); ?>">Newer &rarr;</a>
            </li>
        </ul>
        <?php } ?>

    </div>

1 个回复
SO网友:Michael

尝试改编法典中的代码https://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query 要适合您自己的查询:

        <a href="<?php echo get_next_posts_link( \'Older Entries\', $news->max_num_pages ); ?>">&larr; Older</a>

相关推荐

change pagination url

目前,我的分页页面URL为:http://www.example.com/category_name/page/2但我需要将此URL结构更改为:http://www.example.com/category_name/?page=2等有什么解决方案吗?