Site logo

Archived topic

Custom Category Pages

5 replies · Started by Vish on December 20, 2019

Viewing posts 1–6 of 6

Hi Tom

So, I have been digging around ways to make custom category pages. Figured out that it can be done with WP Show Posts (I have the Pro version too).

I came across this help doc: Use Static Pages as Category Archives

Now, If I follow the above method, I would have to make static category pages for all my categories. The problem is I have too many categories and it does not seem feasible. I want the above method to work for only one category. Is there a way to do it by hardcoding the category name or something?

Hi there,

Try this:

add_filter('request', function( array $query_vars ) {
    if ( is_admin() ) {
        return $query_vars;
    }

    if ( isset( $query_vars['category_name'] ) ) {
        $pagename = $query_vars['category_name'];

        if ( get_page_by_title( $pagename ) ) {
            $query_vars = array( 'pagename' => "$pagename" );
        }
    }

    return $query_vars;
} );

It will only work if the page actually exists.

Tom

It's not working. Maybe I am doing something wrong? Here's what I did:

This is my category URL: https://randomdomain.com/category/generatepress-themes
I have created a page with WP Show Posts shortcode. This is the URL: https://randomdomain.com/generatepress-themes

I tweaked your code to add the category name like below and added it as a snippet using Code Snippets.

add_filter('request', function( array $query_vars ) {
    if ( is_admin() ) {
        return $query_vars;
    }

    if ( isset( $query_vars['generatepress-themes'] ) ) {
        $pagename = $query_vars['generatepress-themes'];

        if ( get_page_by_title( $pagename ) ) {
            $query_vars = array( 'pagename' => "$pagename" );
        }
    }

    return $query_vars;
} );

The only change I made is replacing the category_name with generatepress-themes

My code is actually complete - no need to change it at all :)

My bad! I used your code as-is without any changes and it still didn't work.

Here's some more information:
My Category Page URL:https://www.example.com/category/wordpress-themes
My Normal Page URL:https://www.example.com/wordpress-themes

I simply copy/pasted your code to code snippets and set it to run everywhere.

Kindly let me know if I am doing something wrong. :)

This archived topic is closed to new replies.