Wordpress Custom Category Pages Show the Same Posts on All Paginated Pages

Joined
May 14, 2022
Messages
76
Likes
54
Degree
0
I got an issue with the pagination on the category pages I recently modified.
All /category/page/2/ pages show the same content as /category/. Can't figure out why.

To be clear, I do not use /category/ in my URL, but domain/category-topic/.
So domain/category-topic/page/2/ shows the same content as domain.com/category-topic/

I added this code to my functions php to replace the categories with custom pages:
PHP:
add_filter('request', function(array $query_vars) {
    // do nothing in wp-admin
    if(is_admin()) {
        return $query_vars;
    }
    // check if the query is for a category archive
    if(isset($query_vars['category_name']) && !isset($query_vars['name'])) {
       
      //generate the page path
        $pagename = $query_vars['category_name'];
     
      //attempt to load the page matching the $pagename slug
        $page = get_page_by_path( $pagename , OBJECT );

     if ( isset($page) ){
        // completely replace the query with a page query
        $query_vars = array('pagename' => "$pagename");
     }
    }
    return $query_vars;
});

Of course I Googled but I could not find a solution that works. I think it should be something like this:
PHP:
add_filter('redirect_canonical','pif_disable_redirect_canonical');

function pif_disable_redirect_canonical($redirect_url) {
    if (is_singular()) $redirect_url = false;
return $redirect_url;
}

Anyone an idea?
 
Last edited:
I've had this issue in the past and can't immediately remember what the problem was. But I can tell you that we'll need to see your custom category loop itself with all the query arguments you've set up.

A part of me thinks it has something to do with using the offset argument, if you are using one, and having to feed the loop specific math to get it to "skip" to the right starting post. So something like $offset = $posts_per_page * ($paged - 1). So if you're showing 25 posts per page, the first page would start at 0 and show the first 25 posts. Then page 2 would start at 25 and go to 50, and so forth, as it should.

Another possibility is failing to feed the query arguments array the $paged variable, which isn't necessary in normal loops on normal categories but when you're writing a custom loop on a static page, you need it. It's also easy to give it the wrong info. So like $paged = get_query_var('paged') ? get_query_var('paged') : 1;, meaning if the paged variable exists, then set $paged to it, otherwise set $paged to 1. To say it another way, if you're on a paginated page (paged) then set $paged to that page number, otherwise you're on the first page of the paginated series / category. Then you set the $args[paged] = $paged or $args = array( 'paged' => $paged). Whatever fits how you have it set up.

I know this is a common problem on custom category pages, custom post type categories, and the homepage. Let's see that loop!
 
I've had this issue in the past and can't immediately remember what the problem was. But I can tell you that we'll need to see your custom category loop itself with all the query arguments you've set up.

A part of me thinks it has something to do with using the offset argument, if you are using one, and having to feed the loop specific math to get it to "skip" to the right starting post. So something like $offset = $posts_per_page * ($paged - 1). So if you're showing 25 posts per page, the first page would start at 0 and show the first 25 posts. Then page 2 would start at 25 and go to 50, and so forth, as it should.

Another possibility is failing to feed the query arguments array the $paged variable, which isn't necessary in normal loops on normal categories but when you're writing a custom loop on a static page, you need it. It's also easy to give it the wrong info. So like $paged = get_query_var('paged') ? get_query_var('paged') : 1;, meaning if the paged variable exists, then set $paged to it, otherwise set $paged to 1. To say it another way, if you're on a paginated page (paged) then set $paged to that page number, otherwise you're on the first page of the paginated series / category. Then you set the $args[paged] = $paged or $args = array( 'paged' => $paged). Whatever fits how you have it set up.

I know this is a common problem on custom category pages, custom post type categories, and the homepage. Let's see that loop!
Thank you for the repsonse Ryuzaki! I've been trying to fix this all day, without results so far (instead of working lol).

Let's be clear on what I have done:
- Created pages with the same URL as the category pages.
- Copied above snippet to the functions.php of my child theme.

Result: when I type in domain/category-name/ I get my custom page (just a normal page) and when you click on the category in the breadcrumbs you also go to the same page.

Maybe it is the problem that I did not customize it more. On the category page (new page) I use a grid where I can also fill-in the pagination (its a block from a plugin). Before, this method worked. Now, the amount of posts get capped on the number I fill in the plugin, but every page starts at the first post (and therefore is the same).

Apart from the above snippet in the child functions.php I did not modify any code. I use the Twenty Twenty theme, so you can find the code here. Sorry, not trying to be lazy but I think you can find it more easily like this than when Im going to post random code snippets.

I think you are right that we have to add the math to skip the first X posts. Before, I could modify this in the front end (via the grid plug), but that doesnt seem to work anymore.

Thank again for all the support and help so far, I'm sure I can return the favor (for this forum) in the future.
 
Last edited:
I think I understand what you’ve done. Ultimately you’re loading static pages instead of archive templates of any sort, which messes with the pagination capabilities. Is this right?

This is kind of me asking if there isn’t an easier way to do this. Are your custom categories so complex that you couldn’t get it done with either the built in “category description” functionality (HTML & CSS but no PHP), or for more complex options using custom category templates or altering the provided one in a child theme?

Did you do it to get the grid layout and have access to the Gutenberg editor? What’s the goal of these custom pages? If you’re willing to do some CSS you could replicate the grid even in the normal category template.

I’m wondering if you can’t sidestep a lot of this and return to Wordpress’s intended loop usage. It def gets whacky when you get away from archives. It’s how it’s built unfortunately.
 
Yes, that's correct. I am using 'normal' static pages as category page.

My category pages are not complex all, I think. Its basically some text and the grid.
I did it this way because before breadcrumbs, I did not really use the category pages. I used the static category pages and that was it (probably not good practice). When I started to use breadcrumbs, I wanted to keep the pretty grid and decided to replace the category archives with the static pages.

You are saying to modify the category archives, instead of replacing them? I did not think of this as an option lol. Especially since you cannot edit the category pages like a normal page.

How could I replicate the grid with CSS? I'm familiar with CSS but I think you need also HTML to do this?
 
Last edited:
Its basically some text and the grid.
This is what I was getting at. You can do this without custom static pages or even page templates.

They've apparently changed things around quite a bit with these block themes which I haven't used and are trying to be super streamlined in their template hierarchy usage. So in this theme I don't see a category.php or even an archive.php, which makes me think they're using the index.php for categories. I open it and see that's the case. It's being used for search results and more.

Within that index.php you find things like "if not the homepage, then get_the_archive_description" which then does some more if-checks to determine it's a category and should grab the category description. I'm not entirely sure what it's going to look like on these pages, but these descriptions come before the loop so this text or whatever you add to it will appear above the grid. Then below the grid they get the template-part for the pagination.

So my point here is that you should be able to achieve what you're wanting without any custom functionality. It's all built in. You shouldn't need an extra plugin to get the pagination working or anything.

The only thing that seems like it'd be different is how you display the posts in the category. If you want a grid, I'm nearly certain you could pull it off by overriding the CSS for category pages only.

Wordpress will add CSS classes in the <body> depending on what page you're on, among other things. It might be <body class="archive category blog"> or even have the name of your category like "category-motorcycles" to where you can really dial down which archive you're affecting. It should even have the ID of the category like "category-4".

From here you'll have to know how to design the grid with CSS. There's multiple ways of doing it with floats or inline-blocks or even (probably the best now) would be flexbox. If you aren't already familiar with CSS it's going to be a huge to-do because you're not starting from scratch, you're overriding existing CSS to so there will be a lot of setting attributes to their default values, media queries for responsivity, etc.

But my point was, if you only want to affect categories and not the homepage or search pages or anything else using index.php then you'll need to prepend all of these CSS attributes you're targeting with .category.

It MIGHT be the case that they let you design categories with the block editor too. I've not used these new-fangled themes. But if they ask you to assign a content template, then they may expect you to use the block editor and you could slap your grid right back in there, assuming it's some kind of block, then let their normal pagination do its job. This is a big "MIGHT" and I doubt it's the case. I just haven't used them so I don't know.

Another angle to look at all of this is "do I really need the grid at all?". I did find a legit category on the demo and I could see why you would want it, but is it anything that's going to affect the revenue? Unless I'm coding up my entire theme from the ground up, which I do... If I'm going to select a theme to use, I'm going to accept what it gives me. Trying to get too far into the weeds with pre-made themes ends up with a cluster-hmm-hmm that often comes with extra page load speed implications. Like that custom pagination block probably comes from a plugin that requires loading extra CSS code just for that pagination block. It might be minor but it's an example of the extra overhead you take on by getting custom with something that wasn't meant for it.

Anyways, I hope this is helpful. I am afraid you're heading off down the wrong path when backtracking closer to what the theme is from the start is the right move, and that's including if you want to alter the category pages. I think it can be done as it is, using the existing functionality and then some CSS work.
 
Thank you for this huge reply Ryuzaki, I appreciate it!

You are totally right that you should deal with the theme if you choose one. In my defense, I didnt know shit about websites / wordpress at all when I started. For the next website I'll think twice obviously.

I just compared the grid I have now with the standard category page. I have to say, it's a big difference. The page I use now is perfect, I think. You are right, I don't know how much it will affect revenue but I'm sure it will downgrade the website.

To be honest, I dont know if I'm ready to give up the current category pages..

I think I have 4 options:
- Use the inferior category pages and keep the breadcrumbs (which I also love).
- Ditch breadcrumbs (how much will this effect revenue?) and go business as usual with the static pages.
- Keep breadcrumbs + static pages but get rid of the pagination. Now, with 50 posts on 1 page I still get 100/100 on pagespeed. What are drawbacks of using 400 posts / page? Or maybe a 'load more'.
- Change theme, but this will also come with a lot of complications.

What are your thoughts on this?
 
If it's what you want, I don't see the problem going after it. This issue can be overcome, but it's going to require more work (obviously!) and maybe lead to more unintended consequences.

I recall having this problem on a homepage once and solving it, only to find that I could now visit any made up number of pagination, like /page/82739/ and the first page would load. It would work correctly until it ran out of posts and then would just show page one. It's no problem until Google finds it all and indexes it. And say they find one of them and you have "prev" and "next" schema or <head> links going on and they discover it all, endlessly.

You have paths forward, which seem to be to tweak the loop code (pain in the butt) or revert back to the standard category page and tweak it with CSS (learning experience). Or just keep what you had.

The breadcrumbs don't really matter anywhere but on the post pages, in my opinion. I don't think they'll effect your revenue in a noticeable way. It's cool to have them show in the SERPs instead of just a raw URL, and that could lightly effect your CTR.

I would not attempt to load 400 posts on a page. I would if I had the "load more" thing going on, but now we're talking about extra Javascript and pinging of the database (I'm sure it can be cached to some degree though).

I wouldn't change the theme over this. I'd encourage you to use it as designed before I said to change it altogether. There's also no reason you can't create a child theme and slap in a category.php and do whatever the hell you want. You could get back to a standard loop, tweak the HTML and CSS however you want, and create your grid. But you can do that without a custom category template, too, with just CSS, though you may end up loading larger images and showing them smaller in the grid with this theme loading the big boys on the default category view.

I don't think any of these choices are particularly important, (not that you asked). If your path is to learn how to really work with the backend of Wordpress or to code themes, then it's 100% worth your time to wrestle with it. If that's not your path, then you're better off publishing more, improving content, marketing, gaining links, etc. You can look in your analytics and see how much traffic your category pages actually get and let that inform you.
 
Thank you Ryuzaki! I did choose the CSS option and was working on it this weekend. Then, I stumbled upon an option to make the grid on the default category page, completely hidden within a plugin I already had... With some CSS it is now as good as it was before. Problem solved. Thanks for the guidance, thats how I ultimately found the solution.

The breadcrumbs don't really matter anywhere but on the post pages, in my opinion. I don't think they'll effect your revenue in a noticeable way. It's cool to have them show in the SERPs instead of just a raw URL, and that could lightly effect your CTR.

You mentioned before that an advantage of breadcrumbs is the increased CTR. I personally thought the breadcrumbs would increase time-on-site / dwell time which is a ranking factor I believe. Number of pages per sesison goes up probably (due to easier navigation) which will affect those (light) ranking factors.

I don't think any of these choices are particularly important, (not that you asked).
Hehe, appreciate all the feedback. Defenitely good to keep focusing on the things that matter most.
 
Back