Skip Navigation

[Resolved] Customizing Post Type Category Permalink/ post-type/%category%/%postname%/

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by carlosA-12 1 year ago.

Assisted by: Waqar.

Author
Posts
#2601569

Tell us what you are trying to do?

*My post type base is /portfolio/
*My category slug is /portfolio-category/
*When Im viewing a category, I need the permalink to be /portfolio/category-name
*When Im viewing a single post, it should be /portfolio/category-name/post-name

- At the very least, I need the category permalink to work as described. It's confusing if I have / portfolio/ listing all my categories, but when you view a category, it's currently not /portfolio/ but /portfolio-category/cat-name. It just does not make for a consistent URL structure.

Is there any documentation that you are following?
I wasn't able to find any.

#2601893

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

custom-post-type-settings.png
taxonomy-settings.png

Hi,

Thank you for contacting us and I'd be happy to assist.

The permalink structure that you described is what is available out of the box and is considered conventional.

On my test website, I was able to achieve the changes you're planning, through these steps:

1. In the 'portfolio-category' taxonomy's settings, I added the following text in the Rewrite field:
/portfolio

( screenshot: taxonomy-settings.png )

After this change, the taxonomy archive page URL will become
/portfolio/category-slug

2. In the 'portfolio' post type's custom URL rewrite settings, I included:
/portfolio/%portfolio-category%

( screenshot: custom-post-type-settings.png )

3. Next, I include the following custom code, in the active theme's 'functions.php' file:


function change_link_portfolio( $post_link, $id = 0 ) {
    $post = get_post( $id );
    if( $post->post_type == 'portfolio' ) 
    {
        if ( is_object( $post ) ) {
            $terms = wp_get_object_terms( $post->ID, array('portfolio-category') );
            if ( $terms ) {
                return str_replace( '%portfolio-category%', $terms[0]->slug, $post_link );
            }
        }
    }
    return   $post_link ;
}
add_filter( 'post_type_link', 'change_link_portfolio', 99, 3 );
 
function generated_rewrite_rules_portfolio() {
    add_rewrite_rule(
        '^portfolio/(.*)/(.*)/?$',
        'index.php?post_type=portfolio&name=$matches[2]',
        'top'
    );
}
add_action( 'init', 'generated_rewrite_rules_portfolio' ); 

This code finds the '%portfolio-category%' string from the single portfolio page's permalink and replaces it with the attached 'portfolio-category' term's slug.

4. After making this change, please flush WordPress rewrite rules by clicking the "Save Changes" button at WP Admin -> Settings -> Permalinks and then check that permalinks of these pages.

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2602199

After making the changes, the links did update but it broke all breadcrumbs.
I was using rankmath and this is what it showed for the portcolio link when on a single post.
href="hidden link"
If I turn on Kadence default bread crumb it shows the same link.

#2602209

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing this update.

The downside of having this unconventional permalink structure is that you're likely to run into compatibility issues such as this with other plugins and themes too.

You can get in touch with the Rankmath plugin's support team to see if they offer any customization filter to replace '%portfolio-category%' part with the attached term's slug.

Alternatively, you can switch to the Kadence theme's breadcrumbs.

#2602325

I do appreciate you trying to help with this.
The issue arises with any breadcrumb, I did say that I tried the kadence breadcrumb.

When you said to go to the post type and put /portfolio/%portfolio-category%/
This also effects the default post type url to become /portfolio/%portfolio-category%/
I wrote all kinds of various code that would cause the breadcrumb to recognize %portfolio-category%
But this made the breadcrumb look like <Home> -> Portfolio ( ahref /portfolio/cabinet painting) -> Cabinet Painting ( ahref /portfolio/cabinet painting)
You see, Both the Portfolio Base and the Category displayed the same link. The breadcrumb cannot figure out how to remove /%portfolio-category%/ for the root portfolio page since we put this in the post type permalink settings /portfolio/%portfolio-category%/

#2602359

Im going to abandon trying to get this to work. People have wanted this from WordPress for ever, its just too complicated without using a special plugin like permalink manager pro.

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.