【问题标题】:Wordpress Pagination with custom query loads same content on every page带有自定义查询的 Wordpress 分页在每个页面上加载相同的内容
【发布时间】:2014-06-23 03:43:42
【问题描述】:

我正在使用 2 个自定义查询在自定义 Wordpress 模板主页上加载帖子。 第一个查询加载 4 个“粘性”帖子,下一个查询加载几个最近的帖子。

这很好用,可以满足我的需要,但是点击时分页链接在每个页面上显示相同的内容。

例如,我在主页上有 10 个帖子,当我点击第 2 页时 - 完全相同的 10 个帖子再次出现。如果我点击分页访问第 3 页,同样的 10 个帖子再次等。 无论我在哪个页面上,都会列出相同的 10 个帖子。

我已经阅读了一些关于需要分页的内容,但是几次尝试将其包含在下面的代码中都失败了。

任何想法表示赞赏!

我的代码...

<?php $sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 4,
'post__in'  => $sticky,
'ignore_sticky_posts' => 1,
); ?>

<?php $the_query = new WP_Query( $args ); ?>

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

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php the_permalink() ?>"<?php the_title(); ?>"></a>
<?php endwhile; ?>

<?php wp_reset_postdata(); ?>

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

<main id="main" class="site-main" role="main">

<?php $the_query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<?php get_template_part( 'content', get_post_format() ); ?>

<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>

【问题讨论】:

    标签: php loops pagination wordpress


    【解决方案1】:

    您确实需要$paged 参数(docs)。未经测试但试一试:

    <?php
    // $paged holds the current page offset
    $paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
    
    /**
     * - Checks if the current page is 'paged' (false on first page)
     * - Remove the check if you need those sticky posts on all pages
     * - I added the $paged parameter so those sticky posts will paginate 
     *   if you decide to show them on all pages
     * 
     */
    if( ! is_paged() ) { // Sticky posts only on first page
        $sticky = get_option( 'sticky_posts' );
        $args = array(
            'posts_per_page' => 4,
            'post__in'  => $sticky,
            'ignore_sticky_posts' => 1,
            'paged' => $paged // ah, the page offset
        );
        $the_query = new WP_Query( $args );
        if ( $the_query->have_posts() ) {
            while ( $the_query->have_posts() ) {
                $the_query->the_post();
                ?>
                <a href="<?php the_permalink() ?>"><?php the_title(); ?>"></a>
                <?php
            }
        } else {
            ?>
            <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
            <?php
        }
        wp_reset_postdata();
    }
    ?>
    
    <main id="main" class="site-main" role="main">
    
        <?php
        $args = array(
            'post__not_in' => get_option( 'sticky_posts' ),
            'paged' => $paged // ah, the page offset
        );
        // can be done via new WP_Query but today I am lazy
        // also see http://codex.wordpress.org/Function_Reference/query_posts
        query_posts($args);
        if ( have_posts() ) {
            while ( have_posts() ) {
                the_post();
                get_template_part( 'content', get_post_format() );
            }
        } else {
            get_template_part( 'content', 'none' );
        }
        wp_reset_query();
        ?>
    

    【讨论】:

    • 谢谢。我想我已经根据您的上述答案工作了,但是对于一个小问题 - 分页内容的最后一页显示“未找到任何内容”。 Wordpress 设置设置为每页列出 10 个帖子。我一共有20个帖子。 10 在第 1 页显示。 10 在第 2 页显示,由于某种原因,我得到了第 3 页的分页按钮,没有内容。任何想法为什么?在此处查看分页的第 3 页...deadmanfishing.net/page/3
    • 很高兴它有帮助。在那个页面上,我看到了四个便笺和一个帖子。似乎工作正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多