Site logo

[Resolved] Question about Using Static Pages as Category Archives

Home Forums Support [Resolved] Question about Using Static Pages as Category Archives

Home Forums Support Question about Using Static Pages as Category Archives

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #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

    #2244535
    Fernando
    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!

    #2244547
    Paul

    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!

    #2244557
    Fernando
    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!

    #2367293
    Lionel

    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!

    #2367591
    David
    Staff
    Customer Support

    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.

    #2367762
    Lionel

    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?

    #2368366
    Fernando
    Customer Support

    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.

    #2368689
    Lionel

    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!

    #2368952
    David
    Staff
    Customer Support

    We don’t have an answer for that.
    You may find some bright spark on StackExchange that has a way to overcome that issue.

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.