最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - paged variable not working for categorycustom post archive

programmeradmin0浏览0评论

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

Share Improve this question edited May 2, 2019 at 11:44 norman.lol 3,2413 gold badges30 silver badges35 bronze badges asked Nov 2, 2013 at 17:15 wafflwaffl 2077 silver badges15 bronze badges 11
  • 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
 |  Show 6 more comments

1 Answer 1

Reset to default -2

Can you try this?

$paged = (get_query_var('page')) ? get_query_var('page') : 1;
发布评论

评论列表(0)

  1. 暂无评论