This question has asked many time before and none has answers my question. I went through most of the answers before post this.
我在主页中有分页查询。分页正在显示,我可以浏览页面。但每个分页链接都显示相同的帖子。
这是我目前的代码。有人能在这里详细说明错误吗?
<?php
$paged = ( get_query_var(\'page\') ) ? get_query_var(\'page\') : 1;
$args = array(
\'post_type\'=>\'post\',
\'posts_per_page\'=>10,
\'orderby\'=>\'date\',
\'order\'=>\'DESC\',
\'offset\'=>3,
\'paged\'=>$paged
);
$blogs = new WP_Query($args);
if($blogs->have_posts()):
while($blogs->have_posts()): $blogs->the_post();
?>
<div class="col-md-6">
<div class="single-post">
<div class="thumb">
<a href="<?php the_permalink(); ?>">
<img src="<?php the_post_thumbnail_url(\'blog-thumbnail\'); ?>" alt="<?php the_title(); ?>" class="img-fluid" />
</a>
</div>
<div class="content">
<div class="category">
<?php
$categories = get_the_category();
foreach ($categories as $key => $category) {
echo \'<a href="\' . get_category_link($category->term_id). \'">\'. $category->name. \'</a>\';
}
?>
</div>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="metadata">
<span class="date"><?php echo get_the_date(); ?></span>
<span class="author"><?php echo get_the_author(); ?></span>
</div>
<div class="excerpt">
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php
endwhile;
echo \'<div class="paginate-wrap">\'. paginate_links() . \'</div>\';
endif;
wp_reset_query();
?>
提前感谢
SO网友:user2584538
找到了解决方案。有人面对同样的问题吗?请检查这个。
我的代码是对的,但因为offset
参数分页不起作用。我找到了解决方案here.
这是我最后的代码:
<?php
$paged = ( get_query_var(\'page\') ) ? get_query_var(\'page\') : 1;
$per_page = 10;
$defualt_offset = $offset;
if ($paged == 1) {
$offset = $default_offset;
} else {
$offset = (($paged - 1) * $per_page) + $default_offset;
}
$args = array(
\'post_type\'=>\'post\',
\'posts_per_page\'=>$per_page,
\'orderby\'=>\'date\',
\'order\'=>\'DESC\',
\'offset\'=>3,
\'paged\'=>$paged
);
$blogs = new WP_Query($args);
if($blogs->have_posts()):
while($blogs->have_posts()): $blogs->the_post();
?>
<div class="col-md-6">
<div class="single-post">
<div class="thumb">
<a href="<?php the_permalink(); ?>">
<img src="<?php the_post_thumbnail_url(\'blog-thumbnail\'); ?>" alt="<?php the_title(); ?>" class="img-fluid" />
</a>
</div>
<div class="content">
<div class="category">
<?php
$categories = get_the_category();
foreach ($categories as $key => $category) {
echo \'<a href="\' . get_category_link($category->term_id). \'">\'. $category->name. \'</a>\';
}
?>
</div>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<div class="metadata">
<span class="date"><?php echo get_the_date(); ?></span>
<span class="author"><?php echo get_the_author(); ?></span>
</div>
<div class="excerpt">
<?php the_excerpt(); ?>
</div>
</div>
</div>
</div>
<?php
endwhile;
echo \'<div class="paginate-wrap">\'. paginate_links() . \'</div>\';
endif;
wp_reset_query();
?>