Archived topic
Question about Using Static Pages as Category Archives
9 replies · Started by Paul on June 5, 2022
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
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!
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!
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!
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!
Hi there,
how many sub-categories are you dealing with ? As the simplest solution would be to include the sub-category slugs in $targets array.
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?
WordPress doesn't allow identical slugs which is why a -2 is 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.
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!
We don't have an answer for that.
You may find some bright spark on StackExchange that has a way to overcome that issue.