Query post Pagination Problem

时间:2015-04-05 作者:webHasan

我正在使用以下代码进行查询帖子,但我没有得到分页链接。以下代码的问题在哪里。

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

    <?php 

        $arr = array(
            \'post_type\'     => \'post\',
            \'posts_per_page\' => 3
        );

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

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

        <?php if ( is_home() && ! is_front_page() ) : ?>
            <header>
                <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
            </header>
        <?php endif; ?>

        <?php
        // Start the loop.
        while ( $the_query->have_posts() ) : $the_query->the_post();
            get_template_part( \'content\', get_post_format() );

        // End the loop.
        endwhile; ?>
        <div class="nav-previous alignleft"><?php  next_posts_link(\'Older Post\'); ?></div>
        <div class="nav-next alignright"><?php previous_posts_link( \'Newer posts\' ); ?></div>
    <?php

    wp_reset_postdata();

    endif;
    ?>

    </main><!-- .site-main -->
</div><!-- .content-area -->

1 个回复
最合适的回答,由SO网友:webHasan 整理而成

我去解决这个问题。我取了变量名,比如$wp\\U query=new wp\\U query($arr);instate of$the\\u query=新的WP\\u query($arr);所以我的工作代码应该是

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

    <?php 
        // global $wp_query;

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

        $arr = array(

            \'post_type\'     => \'post\',
            \'posts_per_page\' => 3,
            \'paged\' => $paged
        );

        $wp_query = new WP_Query($arr);
     ?>

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

        <?php if ( is_home() && ! is_front_page() ) : ?>
            <header>
                <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
            </header>
        <?php endif; ?>

        <?php
        // Start the loop.
        while ( $wp_query->have_posts() ) : $wp_query->the_post();
            get_template_part( \'content\', get_post_format() );

        // End the loop.
        endwhile; ?>
        <div class="nav-previous alignleft"><?php  next_posts_link(\'Older Post\'); ?></div>
        <div class="nav-next alignright"><?php previous_posts_link( \'Newer posts\' ); ?></div>
    <?php

    wp_reset_postdata();

    endif;
    ?>

    </main><!-- .site-main -->
</div><!-- .content-area -->

结束

相关推荐

Pagination and Related Posts

是否可以对嵌入在single中的以下相关POST代码使用分页。我的主题的php。 <?php // for use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { $first_tag = $