[Support request] Linking to custom category page

Home Forums Support [Support request] Linking to custom category page

Home Forums Support Linking to custom category page

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #1095460
    Paul

    Looking to hard code links to my custom category pages and avoid the hop with a 301.

    I did something similar here with custom author pages – https://generatepress.com/forums/topic/linking-to-custom-author-archive/ which works great.

    is it possible to do something similar for categories?

    Thanks

    #1095777
    David
    Staff
    Customer Support

    Hi there,

    maybe this doc from WP Show Posts is what you’re looking for:

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

    #1095814
    Paul

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

    #1096528
    David
    Staff
    Customer Support

    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;
    } );
    #2010194
    Hendrik

    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”

    🙁

    #2010250
    Hendrik

    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? 🙁

    #2010788
    David
    Staff
    Customer Support

    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 ?

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