Site logo

Archived topic

Changing Category URL to Custom Category URL

4 replies · Started by Brian on March 10, 2023

Viewing posts 1–5 of 5

Hi, I currently have a breadcrumb in my posts header element that displays the category name (and links to the default WP category URL) using

		<div class="meta-category">{{post_terms.category}}
		</div>

I've created some custom category pages (as actual pages) and would like to replace the default WP category URL with the custom URL for each category page.

I've also created a custom field for each category (using ACF) where I can enter the custom URL attached to each of my categories.

Now, the crunch part, is there a way to replace the default WP category URL with my custom category URL e.g. strip the default URL out of the {{post_terms.category}} and replace it with my custom one?

Hi there,

replacing the default term link i am not sure on, as messing with URLs can get ...well ... messy :)

There is the term_link hook which according to the codex could do what you need:

https://developer.wordpress.org/reference/hooks/term_link/

I smashed this snippet together, which swaps the $termlink for your_custom_field ACF field.


function db_acf_term_link( $termlink, $term, $taxonomy ){
    if( 'category' == $taxonomy ) {
        $replaceURL = get_field('your_custom_field', $term);
        if ( $replaceURL ) {
            $termlink = $replaceURL;
        }
    }
    return $termlink;
}
add_filter( 'term_link', 'db_acf_term_link', 10, 3 );

That if it works will replace wherever get_term_link isn used, which i believe is in that header element template tag.

Thanks David,

Just tried your snippet, but it doesn't seem to be replacing the term link with my custom one.

I've updated your snippet with the ACF field I've created but no dice I'm afraid - the breadcrumb is still linking to the default WP category page rather than my custom one.

//* Swap category URL for custom category URL
function db_acf_term_link( $termlink, $term, $taxonomy ){
    if( 'category' == $taxonomy ) {
        $term = get_queried_object();
        $replaceURL = get_field('custom_category_page_URL', $term);
        if ( $replaceURL ) {
            $termlink = $replaceURL;
        }
    }
    return $termlink;
}
add_filter( 'term_link', 'db_acf_term_link', 10, 3 );

I guess I should still be using {{post_terms.category}} to call the category name/link?

Also, as the custom field (custom_category_page_URL) is associated with the category itself, rather than the post - does it need to be passed from the category to the post for the above hook to work?

All good, figured it out with some help from Chat-gpt.

So this code passes the custom URL from the category page to the post, then switches the default WP category term_link for my custom one:

//* Swap category URL for custom category URL
function custom_term_link( $termlink, $term, $taxonomy ) {
    // Check if the term belongs to your_taxonomy
    if ( $taxonomy == 'category' ) {
        // Get the custom_category_link value for the current term
        $custom_link = get_field( 'custom_category_page_URL', $term );
        if ( $custom_link ) {
            // If the custom_link field has a value, use it as the link for the post
            $post_id = get_queried_object_id();
            $termlink = $custom_link;
        }
    }
    return $termlink;
}
add_filter( 'term_link', 'custom_term_link', 10, 3 );

Ah - i should have smashed it together a little slower as the term_link hook already passes the $term - which makes sense.

Glad to hear you got it working.

This archived topic is closed to new replies.