- This topic has 4 replies, 2 voices, and was last updated 3 years, 3 months ago by
David.
-
AuthorPosts
-
March 10, 2023 at 5:30 am #2562824
Brian
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?
March 10, 2023 at 7:23 am #2562939David
StaffCustomer SupportHi there,
replacing the default term link i am not sure on, as messing with URLs can get …well … messy 🙂
There is the
term_linkhook 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
$termlinkforyour_custom_fieldACF 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_linkisn used, which i believe is in that header element template tag.March 13, 2023 at 12:54 am #2565542Brian
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?
March 13, 2023 at 1:35 am #2565596Brian
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 );March 13, 2023 at 4:03 am #2565758David
StaffCustomer SupportAh – i should have smashed it together a little slower as the
term_linkhook already passes the$term– which makes sense.Glad to hear you got it working.
-
AuthorPosts
- You must be logged in to reply to this topic.