最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom post types - Pagination for a cpt filtered with a category

matteradmin4PV0评论

I was tasked with creating a posts aggregator that fits seamlessly into the category nav-bar. For this to work, I could not use the category-list.php that exists, for it is full of workarounds and exceptions and i can barely make sense of it.

So I went simple and created a CPT (mensuels) to create archives (month/year) of posts based on their date and tags. I added the taxonomies category and post_tag and categorize them as 'themes-mensuels' so that I can include them as part of the category nav-bar in the site header.

I created a file called category-themes-mensuels.php that displays the list of archives and a file called single-mensuels.php that displays the chosen month (one cpt) and lists the articles it aggregated.

The CPT has posts basics plus 2 fields: year and month. When I create one, I fill those in, add a month-year title, check the 'themes-mensuels' category, then choose whatever posts tags came up during that month. I can then add content to comment on such or such particular subject I am filtering for.

I did not create specific pages in the wordpress theme.

This works well:

  • hovering over the bar shows 4 latests "themes" just like they were posts in other categories
  • clicking the tab takes me to the category page and lists all the themes. I had to list their articles below each item (like another CPT on the site) and that works too.
  • choosing one takes me to the single-mensuels page (blog/mensuels/month-year) where there is a bit of a intro showing thumbnail/excerpt/content then lists each posts as per the filter.
  • each article is shown in single.php as expected.

But pagination does not work for either page.

I list the custom-post-type list using the category tag as a category-$slug page. Then I list ONE theme using its single-$posttype page.

The paginate_links() code does its job: the links are there.

But the query_var(paged) is not read, so the category page goes to a 404, and the single-mensuels page (for one theme) just returns the first page of articles.

I looked at other cpt pages on the site and created wp pages for my needs, but pagination was the only thing that worked: category page did not link to category nav-bar (not the "right" way. Though that page showed all the items), and single-mensuels page ignored the new wp_query that was coded in, showing ALL POSTS EVER. So I am not using those.

I tried quite a few things over the past week : renaming the category, changing the number of posts read and removing posts_per_page from the new wp_queries, renaming the file to archive-themes-mensuels.php (and back again)

I save the permalink structure in the settings tab when I change something in the functions.php file.

I cannot change the permalink structure of my site and it uses the word "categorie" as a prefix for the categories.

I am so close to getting this to work, but something in the WordPress logic escapes me and it seems no one has attempted this.

Edit: I realized I could link the nav-bar category tab-link to an archive page for the cpt rather than mistreat the category. So I dutifully reproduced the formatting code for the category (getting links for each theme-post), stuck it in the bar, and directed the tab-link to a renamed category-themes-mensuel.php to archive-mensuels.php.

Now I have pagination for the cpt archive page and better understanding of the template hierarchy. That's 1/2 pagination.

Edit 2:

I DID try several combinations of the global $tempquery = $wp_query switcheroo hack. It didn't work.

Part of the challenge is that there are 3 loops going on in that page, more if you count the header. My custom post type is only just a placeholder for the keys that will be used to query for the posts I really want. My "single-$posttype.php" page is the starting point for another loop once that month's theme comment is displayed. Every article page on the site has a footer containing 3 related articles filtered by tags.

I finally applied the

    /* pagination fix for custom loops on pages*/    
add_filter('redirect_canonical', 'custom_disable_redirect_canonical');
function custom_disable_redirect_canonical($redirect_url){
    if(is_paged() && is_singular('mensuels')) $redirect_url = false; 
return $redirect_url;
}

in functions.php. I was hesitant until I saw I could specify the cpt i wanted. Well, it almost works.

  • If I start from the 1st page, I get nowhere.
  • If I put /page/2 in the url, I get page 2 but can't navigate.
  • If I put in page/3 I get the proper page AND then can navigate back and forth.

I am le dumbfound.

I was tasked with creating a posts aggregator that fits seamlessly into the category nav-bar. For this to work, I could not use the category-list.php that exists, for it is full of workarounds and exceptions and i can barely make sense of it.

So I went simple and created a CPT (mensuels) to create archives (month/year) of posts based on their date and tags. I added the taxonomies category and post_tag and categorize them as 'themes-mensuels' so that I can include them as part of the category nav-bar in the site header.

I created a file called category-themes-mensuels.php that displays the list of archives and a file called single-mensuels.php that displays the chosen month (one cpt) and lists the articles it aggregated.

The CPT has posts basics plus 2 fields: year and month. When I create one, I fill those in, add a month-year title, check the 'themes-mensuels' category, then choose whatever posts tags came up during that month. I can then add content to comment on such or such particular subject I am filtering for.

I did not create specific pages in the wordpress theme.

This works well:

  • hovering over the bar shows 4 latests "themes" just like they were posts in other categories
  • clicking the tab takes me to the category page and lists all the themes. I had to list their articles below each item (like another CPT on the site) and that works too.
  • choosing one takes me to the single-mensuels page (blog/mensuels/month-year) where there is a bit of a intro showing thumbnail/excerpt/content then lists each posts as per the filter.
  • each article is shown in single.php as expected.

But pagination does not work for either page.

I list the custom-post-type list using the category tag as a category-$slug page. Then I list ONE theme using its single-$posttype page.

The paginate_links() code does its job: the links are there.

But the query_var(paged) is not read, so the category page goes to a 404, and the single-mensuels page (for one theme) just returns the first page of articles.

I looked at other cpt pages on the site and created wp pages for my needs, but pagination was the only thing that worked: category page did not link to category nav-bar (not the "right" way. Though that page showed all the items), and single-mensuels page ignored the new wp_query that was coded in, showing ALL POSTS EVER. So I am not using those.

I tried quite a few things over the past week : renaming the category, changing the number of posts read and removing posts_per_page from the new wp_queries, renaming the file to archive-themes-mensuels.php (and back again)

I save the permalink structure in the settings tab when I change something in the functions.php file.

I cannot change the permalink structure of my site and it uses the word "categorie" as a prefix for the categories.

I am so close to getting this to work, but something in the WordPress logic escapes me and it seems no one has attempted this.

Edit: I realized I could link the nav-bar category tab-link to an archive page for the cpt rather than mistreat the category. So I dutifully reproduced the formatting code for the category (getting links for each theme-post), stuck it in the bar, and directed the tab-link to a renamed category-themes-mensuel.php to archive-mensuels.php.

Now I have pagination for the cpt archive page and better understanding of the template hierarchy. That's 1/2 pagination.

Edit 2:

I DID try several combinations of the global $tempquery = $wp_query switcheroo hack. It didn't work.

Part of the challenge is that there are 3 loops going on in that page, more if you count the header. My custom post type is only just a placeholder for the keys that will be used to query for the posts I really want. My "single-$posttype.php" page is the starting point for another loop once that month's theme comment is displayed. Every article page on the site has a footer containing 3 related articles filtered by tags.

I finally applied the

    /* pagination fix for custom loops on pages*/    
add_filter('redirect_canonical', 'custom_disable_redirect_canonical');
function custom_disable_redirect_canonical($redirect_url){
    if(is_paged() && is_singular('mensuels')) $redirect_url = false; 
return $redirect_url;
}

in functions.php. I was hesitant until I saw I could specify the cpt i wanted. Well, it almost works.

  • If I start from the 1st page, I get nowhere.
  • If I put /page/2 in the url, I get page 2 but can't navigate.
  • If I put in page/3 I get the proper page AND then can navigate back and forth.

I am le dumbfound.

Share Improve this question edited Feb 28, 2019 at 14:45 Sassinak asked Feb 26, 2019 at 14:40 SassinakSassinak 216 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Screw using a category. I manually added the tab to the nav-list and disguised it as a category. The posts are filtered by a "special" tag that doesn't need to show up anywhere else. The custom post type looks for that tag and has fields for month and year. Archive-* and single-* template pages display according to plan. Everything works now. Thank you very much to everyone who helped out.

Post a comment

comment list (0)

  1. No comments so far