简体   繁体   English

主页上的分页(WordPress)

[英]Pagination on Home Page (WordPress)

I'm having an issue trying to get pagination to work on the homepage of a site I'm working on. 我在尝试分页以便在我正在工作的网站的主页上工作时遇到问题。

The "Older" and "Newer" links show correctly, and it updates the url to reflect the page number but the posts content remains the same as it does on the first page when flipping through the pages. “较旧”和“较新”链接正确显示,并且更新了URL以反映页码,但帖子内容与翻页时的内容保持不变。 Here is the code I'm using, simplified of course: 这是我正在使用的代码,当然已简化:

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>

Can't seem to figure out what the problem is, any help would be appreciated. 似乎无法弄清楚问题出在哪里,任何帮助将不胜感激。

try to use wp_reset_postdata(); 尝试使用wp_reset_postdata(); instead of wp_reset_query(); 而不是wp_reset_query(); and also updated condition for $paged . 并更新了$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>
?>

Please read this and insert that code of pagination on your page and remove newer and older links 请阅读并在页面上插入该分页代码,并删除较新和较旧的链接

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

Figured out the problem thanks to @milo on the WordPress exchange. 通过WordPress交流平台上的@milo找出了问题所在。 Turns out there are special considerations when using "offset" and pagination. 事实证明,使用“偏移”和分页时需要特别注意。 Check this link for more details . 检查此链接以获取更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM