admin管理员组文章数量:1023782
I am having trouble getting the pagination to work on category archives for a custom post type.
The number of pages/links is accurate, but clicking on the link 404s. (Only in the category archive, pagination is working elsewhere in the site)
The URL is formatted like so:
The Permalink structure is: /%category%/%postname
My loop is as follows (in the archive-project.php
template for the custom post type project
)
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$queryArgs = array(
'post_type' => 'project',
'posts_per_page' => 10,
'paged' => $paged
);
if (isset($project_cat)) {
$queryArgs = array_merge($queryArgs, array(
'category_name' => $project_cat
));
}
$projectQuery = new WP_Query($queryArgs);
The second page is generated as which returns a 404.
The code for my pagination is as follows (Maybe it's not the best route, but it works on every other part of the site, after much headache as well)
global $projectQuery;
$total_pages = $projectQuery->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'show_all' => true,
'format' => '/page/%#%',
'prev_next' => false,
'current' => $current_page,
'total' => $total_pages,
));
}
As mentioned, the page links appear to be accurate, but visiting the Page 2
link gives a 404. Any ideas would be greatly appreciated, I have spent hours on pagination alone, it is a surprisingly complex issue with Wordpress.
I've managed to narrow it down that the paged variable is not working properly here, (get_query_var('paged')) ? get_query_var('paged') : 1;
is always returning 0
although maybe it is due to the formatting of the link.
Additional quick notes:
<-- Works
<-- Works
<-- 404
Reading Settings: Blog pages show at most 1 posts
I am having trouble getting the pagination to work on category archives for a custom post type.
The number of pages/links is accurate, but clicking on the link 404s. (Only in the category archive, pagination is working elsewhere in the site)
The URL is formatted like so: http://domain/projects/category/category_name
The Permalink structure is: /%category%/%postname
My loop is as follows (in the archive-project.php
template for the custom post type project
)
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$queryArgs = array(
'post_type' => 'project',
'posts_per_page' => 10,
'paged' => $paged
);
if (isset($project_cat)) {
$queryArgs = array_merge($queryArgs, array(
'category_name' => $project_cat
));
}
$projectQuery = new WP_Query($queryArgs);
The second page is generated as http://domain/projects/category/category_name/page/2
which returns a 404.
The code for my pagination is as follows (Maybe it's not the best route, but it works on every other part of the site, after much headache as well)
global $projectQuery;
$total_pages = $projectQuery->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'show_all' => true,
'format' => '/page/%#%',
'prev_next' => false,
'current' => $current_page,
'total' => $total_pages,
));
}
As mentioned, the page links appear to be accurate, but visiting the Page 2
link gives a 404. Any ideas would be greatly appreciated, I have spent hours on pagination alone, it is a surprisingly complex issue with Wordpress.
I've managed to narrow it down that the paged variable is not working properly here, (get_query_var('paged')) ? get_query_var('paged') : 1;
is always returning 0
although maybe it is due to the formatting of the link.
Additional quick notes:
http://domain/projects/page/2
<-- Works
http://domain/projects/category/category_name
<-- Works
http://domain/projects/category/category_name/page/2
<-- 404
Reading Settings: Blog pages show at most 1 posts
1 Answer
Reset to default -2Can you try this?
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
I am having trouble getting the pagination to work on category archives for a custom post type.
The number of pages/links is accurate, but clicking on the link 404s. (Only in the category archive, pagination is working elsewhere in the site)
The URL is formatted like so:
The Permalink structure is: /%category%/%postname
My loop is as follows (in the archive-project.php
template for the custom post type project
)
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$queryArgs = array(
'post_type' => 'project',
'posts_per_page' => 10,
'paged' => $paged
);
if (isset($project_cat)) {
$queryArgs = array_merge($queryArgs, array(
'category_name' => $project_cat
));
}
$projectQuery = new WP_Query($queryArgs);
The second page is generated as which returns a 404.
The code for my pagination is as follows (Maybe it's not the best route, but it works on every other part of the site, after much headache as well)
global $projectQuery;
$total_pages = $projectQuery->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'show_all' => true,
'format' => '/page/%#%',
'prev_next' => false,
'current' => $current_page,
'total' => $total_pages,
));
}
As mentioned, the page links appear to be accurate, but visiting the Page 2
link gives a 404. Any ideas would be greatly appreciated, I have spent hours on pagination alone, it is a surprisingly complex issue with Wordpress.
I've managed to narrow it down that the paged variable is not working properly here, (get_query_var('paged')) ? get_query_var('paged') : 1;
is always returning 0
although maybe it is due to the formatting of the link.
Additional quick notes:
<-- Works
<-- Works
<-- 404
Reading Settings: Blog pages show at most 1 posts
I am having trouble getting the pagination to work on category archives for a custom post type.
The number of pages/links is accurate, but clicking on the link 404s. (Only in the category archive, pagination is working elsewhere in the site)
The URL is formatted like so: http://domain/projects/category/category_name
The Permalink structure is: /%category%/%postname
My loop is as follows (in the archive-project.php
template for the custom post type project
)
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$queryArgs = array(
'post_type' => 'project',
'posts_per_page' => 10,
'paged' => $paged
);
if (isset($project_cat)) {
$queryArgs = array_merge($queryArgs, array(
'category_name' => $project_cat
));
}
$projectQuery = new WP_Query($queryArgs);
The second page is generated as http://domain/projects/category/category_name/page/2
which returns a 404.
The code for my pagination is as follows (Maybe it's not the best route, but it works on every other part of the site, after much headache as well)
global $projectQuery;
$total_pages = $projectQuery->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'show_all' => true,
'format' => '/page/%#%',
'prev_next' => false,
'current' => $current_page,
'total' => $total_pages,
));
}
As mentioned, the page links appear to be accurate, but visiting the Page 2
link gives a 404. Any ideas would be greatly appreciated, I have spent hours on pagination alone, it is a surprisingly complex issue with Wordpress.
I've managed to narrow it down that the paged variable is not working properly here, (get_query_var('paged')) ? get_query_var('paged') : 1;
is always returning 0
although maybe it is due to the formatting of the link.
Additional quick notes:
http://domain/projects/page/2
<-- Works
http://domain/projects/category/category_name
<-- Works
http://domain/projects/category/category_name/page/2
<-- 404
Reading Settings: Blog pages show at most 1 posts
- try with re-flush permalinks settings, hope will solve your problem – Anjum Commented Nov 2, 2013 at 17:53
- why are you running a custom query instead of using the default main query? – Milo Commented Nov 2, 2013 at 18:11
-
also -
archive-project
is not the template WordPress will use for a taxonomy term archive, see the Template Hierarchy for taxonomies. – Milo Commented Nov 2, 2013 at 18:16 -
1
@waffl - your question says "category archives for a custom post type", and your example URL that doesn't work appears to be for a taxonomy term. anyway, don't run a custom query, get rid of all that code, run the normal loop, use
pre_get_posts
to alter main queries before they're run- pagination problems solved. – Milo Commented Nov 3, 2013 at 15:11 -
1
Why are you creating a brand new query? Use
pre_get_posts
to modify the main query, your code will be simpler, smaller, and faster! See the comment from @Milo – Tom J Nowell ♦ Commented May 10, 2016 at 19:55
1 Answer
Reset to default -2Can you try this?
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
本文标签: wp querypaged variable not working for categorycustom post archive
版权声明:本文标题:wp query - paged variable not working for categorycustom post archive 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745531988a2154792.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
archive-project
is not the template WordPress will use for a taxonomy term archive, see the Template Hierarchy for taxonomies. – Milo Commented Nov 2, 2013 at 18:16pre_get_posts
to alter main queries before they're run- pagination problems solved. – Milo Commented Nov 3, 2013 at 15:11pre_get_posts
to modify the main query, your code will be simpler, smaller, and faster! See the comment from @Milo – Tom J Nowell ♦ Commented May 10, 2016 at 19:55