admin管理员组文章数量:1122846
I have created a filter to skip first post of the archive on "podcast" category. Below is the code.
add_filter( 'pre_get_posts', 'amd_limit_posts');
function amd_limit_posts($query){
if (is_category('podcast')){
$query->set('posts_per_page', 4);
$query->set('offset', 1);
}
return $query;
}
It is working but on other pages of pagination, it shows same records as on first record. So pagination links (next/prev) are working fine. But the records it is showing are the same as on first page.
How do I fix this?
I have created a filter to skip first post of the archive on "podcast" category. Below is the code.
add_filter( 'pre_get_posts', 'amd_limit_posts');
function amd_limit_posts($query){
if (is_category('podcast')){
$query->set('posts_per_page', 4);
$query->set('offset', 1);
}
return $query;
}
It is working but on other pages of pagination, it shows same records as on first record. So pagination links (next/prev) are working fine. But the records it is showing are the same as on first page.
How do I fix this?
Share Improve this question asked Aug 1, 2017 at 2:44 aslamdoctoraslamdoctor 1894 silver badges15 bronze badges 1- 1 'offset' does not work with pagination - codex.wordpress.org/Class_Reference/… – Michael Commented Aug 1, 2017 at 3:25
1 Answer
Reset to default 0Have a look at the Wordpress Documentation available here, you'll be able to do what you want to do by using two hooks:
https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination
Unfortunately, many developers find out that hard way that setting an offset value in their custom WordPress queries has the nasty and potentially serious side-effect of breaking pagination.
There is a very good reason for this however... the offset argument is actually the value WordPress uses to handle pagination itself. If a developer sets a manual offset value, pagination will not function because that value will override WordPress's automatic adjustment of the offset for a given page.
In order to use an offset in WordPress queries without losing WordPress's pagination features, you will need to manually handle some basic pagination calculations. This can accomplished with the following two hooks
pre_get_posts - This hook allows tweaking of queries before they are run. Here we need to ensure that our offset is applied only to the first page.
found_posts - Allows correction of WordPress's query result count. This allows us to ensure WordPress takes our offset into account for pages other than the first page.
本文标签: paginationSkip first post on Category Archive
版权声明:本文标题:pagination - Skip first post on Category Archive 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304712a1932329.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论