[英]Pagination on Home Page (WordPress)
我在嘗試分頁以便在我正在工作的網站的主頁上工作時遇到問題。
“較舊”和“較新”鏈接正確顯示,並且更新了URL以反映頁碼,但帖子內容與翻頁時的內容保持不變。 這是我正在使用的代碼,當然已簡化:
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>
似乎無法弄清楚問題出在哪里,任何幫助將不勝感激。
嘗試使用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>
?>
請閱讀並在頁面上插入該分頁代碼,並刪除較新和較舊的鏈接
https://www.elegantthemes.com/blog/tips-tricks/how-to-add-pagination-to-wordpress
通過WordPress交流平台上的@milo找出了問題所在。 事實證明,使用“偏移”和分頁時需要特別注意。 檢查此鏈接以獲取更多詳細信息 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.