Site logo

Archived topic

Linking to custom category page

6 replies · Started by Paul on December 6, 2019

Viewing posts 1–7 of 7

Is it possible to adjust this code for when the slugs do not match?

Hmmm.. its not going to be automatic as the names need to be identical - could try conditionally checking a category name and setting a different page name like so:

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

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

        // Check for specicific category name
        if ( $pagename == 'category-slug') {
            // if specific category return specific page
            $query_vars = array( 'pagename' => "my-page-name" );
        } else {
            // else select page that has same name as category slug
            $query_vars = array( 'pagename' => "$pagename" );
        }
    }

    return $query_vars;
} );

I'm in the same boat with the need to use a page instead of the standard archive design.
Using the above script works fine, but what I noticed is that posts within the category that has a static archive page don't render the correct slug.

Example:

"mydomain.com/category/widgets" uses a static page "widgets" in place of the regular category page.

However, what happens is that a post "blue widgets" in the category "widgets" now renders this slug:
"mydomain.com/blue-widgets"

whereas it should be

"mydomain.com/category/widgets/blue-widgets"

:-(

Nevermind ;-)

The problem is actually a different one.

When I use the script from here
https://generatepress.com/forums/topic/linking-to-custom-author-archive/

inside my functions.php, with my permalink settings to "custom structure: /%category%/%postname%/"

the slug for a post in the category "widgets" will render

“mydomain.com/widgets/blue-widgets”

That looks as intended, HOWEVER, the page that gets rendered under that slug is not the post content for "blue-widgets", but the category page (which is using the static page as per enforced by the above script).

Basically, it looks like URL for the post always redirects to the category page.
I'm not using any SEO or redirection plugins, btw.

Would you have any ideas why this is happening? :-(

Hi there,

could you raise a new topic where you can share a link to the site so we can take a look and see whats going on ?

This archived topic is closed to new replies.