Kadence Wordpress Theme: How to Change a URL in the Breadcrumbs for a Redirected Category?

Joined
Apr 22, 2021
Messages
67
Likes
66
Degree
0
Hey lads,

I have 301-redirected my category pages to normal pages.

I.e the redirect goes like this: mydomain.com/category/farts > mydomain.com/farts

Above my posts I have breadcrumbs, or "categories above post", as they call it. The problem is that those links are to the category page. When you click on it, it redirects properly. But I want to minimize the amount of redirects.

So I would prefer those links to be directly to the pages, and not the categories.

I have been Googling for days, but cant find a solution. I was given a filter by the theme author, but that didnt work.

Does anyone know how to change the breadcrumb URLs to avoid the redirect?

Thanks in advance.
 
@Trump. We’d need to know what theme you’re using and see the filter code. Breadcrumbs are not a native Wordpress function so there’s no universal way to manage it. It’ll be on a case-by-case basis.
 
@Trump. We’d need to know what theme you’re using and see the filter code. Breadcrumbs are not a native Wordpress function so there’s no universal way to manage it. It’ll be on a case-by-case basis.
My bad, I should have included that.

It's Kadence theme. The support claimed that this is "changing Wordpress core".

Here is the code he gave me, but it didn't do anything:

Code:
add_filter( 'category_link', 'my_custom_category_override', 20, 2 );
function my_custom_category_override( $termlink, $term_id ) {
if ( 242 == $term_id ) {
$termlink = get_permalink( 934 );
}
return $termlink;
}
 
@Trump, the logic of that makes sense. It's passing in the URL ($termlink) and the Post ID ($term_id) from whatever function is calling it. It's actually passing the Category/Tag ID if we want to be specific here, in this specific case. These ID's are numbers Wordpress assigns to each post, tag, category, page, etc.

Then it's saying "if your Category ID equals 242, change the URL to be the URL of the new page with the ID of 934".

You need to go into your dashboard and confirm these are the correct ID's. You can do this by hovering any link to them, like in your All Posts > Categories page you can hover or copy the Edit link to the category in question and in the URL you should see the ?taxonomy=category&tag_ID=242 as an example. But 242 may not be the ID. You want to swap whatever it is for 242 in that code you provided.

You'll want to get the ID for the page you're redirecting to as well and swap it with 934 if that's not the correct number.

Here's the issue. That only takes care of one of your redirects. You need to copy a portion of your code and do it as many times as needed for each redirect you've created for these categories. It would look like this:

Code:
add_filter( 'category_link', 'my_custom_category_override', 20, 2 );
function my_custom_category_override( $termlink, $term_id ) {
    if ( 242 == $term_id ) {
        $termlink = get_permalink( 934 );
    }
    elseif ( XXX == $term_id ) {
        $termlink = get_permalink( XXX );
    }
    elseif ( XXX == $term_id ) {
        $termlink = get_permalink( XXX );
    }
    return $termlink;
}

Adding the elseif() as many times as you need. That SHOULD work assuming he provided you a function here that actually works with Kadence. The logic makes sense and it's very similar to what you would do with Yoast (I've done that in the past to change the post title in the breadcrumb to just say "Here")

I hope this makes sense and helps. To help further someone would need to install Kadence and toy with it, and I'm not able to do that at the moment. But I hope this puts you on the right path.
 
This is a readability thing but I'd made the following change, especially if you have other developers working with you on your code. It's like saying "if my apple is red" vs "if red is my apple".

This will work:
if ( 242 == $term_id ) {

But you really want to word it like this
if ( $term_id == 242 ) {
 
@Ryuzaki That's awesome! Thanks a lot for taking the time - I appreciate it a lot!

It works now. I have no idea what I could have done wrong the first time. But now it's working. Perhaps I switched places on the term id and termlink.

Thanks again!

@SmokeTree Thanks for your input man. I don't know if I dare change anything now when it works. But I might look into this as well in the upcoming days.

Much appreciated guys!
 
Back