【问题标题】:WP_Query Pagination not working with custom query on custom post typeWP_Query 分页不适用于自定义帖子类型的自定义查询
【发布时间】:2016-07-06 20:46:20
【问题描述】:

我已经阅读了与我类似的许多其他问题,但无法找到解决方法。出于某种原因,我的自定义查询没有分页,我不知道为什么。这可能是一个愚蠢的理由,但希望您能提供帮助。

这是我的代码。

  <?php
            global $wp_query;
            $title = $wp_query->post->post_title;
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            $query = new WP_Query(
                array(
                    'paged' => $paged,
                    'wpse18703_title' => "$title",
                    'posts_per_page' => 10,
                    'post__not_in'  => array($post->ID),
                    'post_type' => array('post', 'features'),

                )
            );

            if ($query->have_posts()){
                while ( $query->have_posts() ) { $query->the_post(); ?>
                    <a href="<?php echo get_permalink(); ?>">
                    <div class="small-12 large-12 gdb-news-container">
                    <div class="small-12 large-4 columns gdb-news-image">
                        <?php the_post_thumbnail('game-news-thumb'); ?>
                    </div>
                    <div class="small-12 large-8 columns game-title">
                        <h5><?php the_title(); ?></h5>
                    <?php echo content(20); ?>
                    </div>
                        <div class="clearfix"></div>
                    </div>
                    </a>

         <?php
                } //end while
                wp_reset_query();
            } //end if
            ?>

            <?php if ( function_exists('reverie_pagination') ) { reverie_pagination(); } else if ( is_paged() ) { ?>
                <nav id="post-nav">
                    <div class="post-previous"><?php next_posts_link( __( '&larr; Older posts', 'reverie' ) ); ?></div>
                    <div class="post-next"><?php previous_posts_link( __( 'Newer posts &rarr;', 'reverie' ) ); ?></div>
                </nav>
            <?php } ?>

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:
    <?php 
        if(isset($_GET['paged']) && !empty($_GET['paged'])):endif;
        global $wp_query;
        $temp = $wp_query;
        $wp_query = null;
        $title = $wp_query->post->post_title;
        $args = array(
            'paged' => $paged,
            'wpse18703_title' => "$title",
            'posts_per_page' => 10,
            'post__not_in'  => array($post->ID),
            'post_type' => array('post', 'features')
        );
        $wp_query = new WP_Query($args);
        if($wp_query->have_posts()){
            while($wp_query->have_posts()){
                $wp_query->the_post(); ?>
                <a href="<?php echo get_permalink(); ?>">
                    <span class="small-12 large-12 gdb-news-container">
                        <span class="small-12 large-4 columns gdb-news-image">
                            <?php the_post_thumbnail('game-news-thumb'); ?>
                        </span>
                        <span class="small-12 large-8 columns game-title">
                            <h5><?php the_title(); ?></h5>
                            <?php echo content(20); ?>
                        </span>
                    </span>
                </a>
            <?php }
        }
        $wp_query = null;
        $wp_query = $temp;
        if(function_exists('reverie_pagination')){reverie_pagination();} else if (is_paged()){ ?>
            <nav id="post-nav">
                <div class="post-previous"><?php next_posts_link( __( '&larr; Older posts', 'reverie' ) ); ?></div>
                <div class="post-next"><?php previous_posts_link( __( 'Newer posts &rarr;', 'reverie' ) ); ?></div>
            </nav><?php
        }
    ?>
    

    我将“small-12”类的 div 更改为 span 的,因为将 div 放入锚标记内是无效的 W3C 代码,因此您可能需要修改您的 css 文件,具体取决于它的编码方式。

    【讨论】:

    • 感谢代码,但我已经尝试过了,仍然没有分页。我真的无法理解为什么它不起作用。
    【解决方案2】:

    我认为您使用 $paged 变量错误(我猜您是在“页面”而不是“帖子”中使用它)。这应该适用于页面和帖子:

    if ( get_query_var( 'paged' ) ) {
        $paged = absint( get_query_var( 'paged' ) );
    } elseif ( get_query_var( 'page' ) ) {
        $paged = absint( get_query_var( 'page' ) );
    } else {
        $paged = 1;
    }
    

    【讨论】:

    • 是的,这也不起作用,我似乎无法弄清楚为什么分页不起作用。它现在显示“页面”,但页面链接只是转到同一页面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多