- This topic has 9 replies, 4 voices, and was last updated 3 years, 6 months ago by
David.
-
AuthorPosts
-
June 5, 2022 at 6:21 pm #2244512
Paul
Hi there, I understand this is possible (Using Static Pages as Category Archives) and I’ve seen the following post (https://docs.wpshowposts.com/article/use-static-pages-as-category-archives/).
With the code in that forum post, all the category archive pages will become static pages. My question is how to only make specific category pages a static page? I want to customize only 1 category page by using a static page (not all category pages).
Any help much appreciated.
Thanks, Paul
June 5, 2022 at 7:31 pm #2244535Fernando Customer Support
Hi Paul,
You can modify your code to something like this:
add_filter('request', function( array $query_vars ) { if ( is_admin() ) { return $query_vars; } if ( isset( $query_vars['category_name'] ) && 'my-category-slug' === $query_vars['category_name'] ) { $pagename = $query_vars['category_name']; $query_vars = array( 'pagename' => "$pagename" ); } return $query_vars; } );Kindly replace my-category-slug with you Category’s slug.
Hope this helps!
June 5, 2022 at 8:04 pm #2244547Paul
Thanks so much! That worked well. What would the code be if I want to the same thing but with more than 1 category (but not all the categories)?
Thanks!
June 5, 2022 at 8:35 pm #2244557Fernando Customer Support
You would need something like this:
add_filter('request', function( array $query_vars ) { if ( is_admin() ) { return $query_vars; } $targets = array( 'my-category-slug', 'my-category-slug-2', 'my-category-slug-3', ); if ( isset( $query_vars['category_name'] ) && in_array( $query_vars['category_name'], $targets ) ) { $pagename = $query_vars['category_name']; $query_vars = array( 'pagename' => "$pagename" ); } return $query_vars; } );Kindly replace my-category-slug, my-category-slug-2, my-category-slug-3 with the slugs, and you may also add more if necessary.
Hope this helps!
October 8, 2022 at 11:28 am #2367293Lionel
Hello, I tried the code above and it works perfect for our parent categories. But we also have some sub categories that we would like to replace with static pages. Is there a way to make this work for our sub categories?
Thanks!
October 9, 2022 at 4:24 am #2367591David
StaffCustomer SupportHi there,
how many sub-categories are you dealing with ? As the simplest solution would be to include the sub-category slugs in
$targetsarray.October 9, 2022 at 7:24 am #2367762Lionel
Hi, There is a few… around 10 sub-categories… I’ve added them to the array..
But the issue i think is in WordPress when I go to create a static page with the same slug name as the sub-category, wordpress adds a “-2” to the pages slug.
So for example…
Existing subcategory is: https://www.mysite.com/christmas/decorations/
When I create a new static page it gets auto renamed to: https://www.mysite.com/christmas/decorations-2
Any Ideas?
October 9, 2022 at 11:55 pm #2368366Fernando Customer Support
WordPress doesn’t allow identical slugs which is why a
-2is added. Can you try changing the slug of your sub categories in Posts > Categories in your dashboard.Then, rebuild permalinks by going to Settings > Permalinks, and hitting Save.
Then create your new static pages.
October 10, 2022 at 6:45 am #2368689Lionel
Hello,
I renamed the subcategory slug and it worked!
But is there another way to solve this, without having to rename subcategories? (this is a LIVE site with hundreds of posts)
I tried to rename again back to the original name, but it added the “-2” again to the static page.
Thanks for your help!
October 10, 2022 at 8:32 am #2368952David
StaffCustomer SupportWe don’t have an answer for that.
You may find some bright spark on StackExchange that has a way to overcome that issue. -
AuthorPosts
- You must be logged in to reply to this topic.