[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 4 posts - 1 through 4 (of 4 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!

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