主页分页(WordPress)

时间:2016-03-11 05:16:44

标签: php wordpress pagination

我遇到了一个问题,试图在我正在处理的网站的主页上进行分页。

“较旧”和“较新”链接正确显示,并更新网址以反映页码,但帖子内容与翻阅页面时第一页上的内容保持一致。这是我正在使用的代码,当然简化了:

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

$get_featured_posts = new WP_Query( array(
'posts_per_page'        => 9,
'post_type'             => 'post',
'ignore_sticky_posts'   => true,
'no_found_rows'         => true,
'paged'                 => $paged,
'offset'                => 5
 ) );

while( $get_featured_posts->have_posts() ):$get_featured_posts->the_post();

<h3 class="entry-title entry-added">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute();?>">    <?php the_title(); ?></a>
</h3>
<p class="short_description"><?php echo short_description('...', 16); ?> </em></p>
<p class="read_more"><a href="<?php the_permalink(); ?>">Read More</a></p>

<?php
     endwhile;
?>
    <?php 
     // Reset Post Data
     wp_reset_query();
     ?>
<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

似乎无法弄清问题是什么,任何帮助都会受到赞赏。

3 个答案:

答案 0 :(得分:0)

尝试使用wp_reset_postdata();代替wp_reset_query();,并更新$paged的条件。

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $get_featured_posts = new WP_Query( array(
    'posts_per_page'        => 9,
    'post_type'             => 'post',
    'ignore_sticky_posts'   => true,
    'no_found_rows'         => true,
    'paged'                 => $paged,
    'offset'                => 5
     ) );

    while( $get_featured_posts->have_posts() ):$get_featured_posts->the_post();

        <h3 class="entry-title entry-added">
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute();?>">    <?php the_title(); ?></a>
        </h3>
        <p class="short_description"><?php echo short_description('...', 16); ?> </em></p>
        <p class="read_more"><a href="<?php the_permalink(); ?>">Read More</a></p>

        <?php
        endwhile;
        // Reset Post Data
        wp_reset_postdata();
        ?>
    <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
?>

答案 1 :(得分:0)

请阅读此内容并在您的网页上插入该分页代码并删除较新旧链接

https://www.elegantthemes.com/blog/tips-tricks/how-to-add-pagination-to-wordpress

答案 2 :(得分:0)

通过WordPress交换中的@milo找出问题。事实证明,使用&#34; offset&#34;时需要特别注意。和分页。 Check this link for more details