Site logo

[Support request] Point a WordPress Category URL to a Custom Category Page

Home Forums Support [Support request] Point a WordPress Category URL to a Custom Category Page

Home Forums Support Point a WordPress Category URL to a Custom Category Page

  • This topic has 7 replies, 4 voices, and was last updated 5 years ago by David.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1743915
    Glenda

    I have created a custom category page layout that I like. https://glendaembree.com/category/chicken

    It is, however, just a page. Is there any way, now that it’s created to point the actual category in WordPress/GP to that new url?

    The page automatically created by WordPress/GP for the category is https://glendaembree.com/category/main-dish. I have not found any way to edit it’s layout to something simple like I’ve done with the first link. Is it even possible to change the url a category points to?

    #1743977
    Elvin
    Staff
    Customer Support

    Hi there,

    Can you try this filter?

    add_filter('request', function( array $query_vars ) {
    
        if ( isset( $query_vars['category_name'] ) ) {
            $pagename = $query_vars['category_name'];
            if ( $pagename != 'your-category-slug') {
                $query_vars = array( 'pagename' => "$pagename" );
            }
        }
    
        return $query_vars;
    } );

    Change the part your-category-slug with the category slug of you’re trying to point to the static page. Make sure the page slug or “pagename” and the category slug are the same.

    #1744032
    Glenda

    Thanks, Elvin. Just to clarify, the only thing I am changing in the filter is the your-category-slug, right?

    #1744041
    Elvin
    Staff
    Customer Support

    Yes that’s right.

    Example:

    add_filter('request', function( array $query_vars ) {
    
        if ( isset( $query_vars['category_name'] ) ) {
            $pagename = $query_vars['category_name'];
            if ( $pagename != 'main-dish') {
                $query_vars = array( 'pagename' => "$pagename" );
            }
        }
    
        return $query_vars;
    } );

    Let us know how it goes.

    #1744060
    Glenda

    I appreciate all your help, Elvin. I haven’t been able to make it work tonight, but Ill keep studying it. Thank you!

    #1745359
    Elvin
    Staff
    Customer Support

    No problem. 🙂

    #1781053
    kasei

    Hey, sorry to jump in. Thanks for the code!

    Is there a quick solution so that it automatically looks for the page instead (or list each category in the same filter)? I’m thinking if we have like 10 or 15 categories, seems like a lot of extra code to copy that whole filter over and over.

    Cheers

    #1781372
    David
    Staff
    Customer Support

    Hi there,

    the original code can be found here:

    https://docs.wpshowposts.com/article/use-static-pages-as-category-archives/

    This ‘redirects’ all Categories to a Page with the same name.

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