It would make a lot more sense if the related products title linked to more of the related products being displayed…
Attempting to change the related products title on the single product page to a link to the end category that correlates to the related products query being displayed.
I have the url slug and name for the end category I just can’t escape the <h2>
tag to turn it into a link. Any advice?
$translated = '<a href="#"> </a>'.esc_html__(end($term_urls).'Continue Browising '.end($term_names), $domain);
add_filter('gettext', 'change_rp_text', 10, 3);
add_filter('ngettext', 'change_rp_text', 10, 3);
function change_rp_text($translated, $text, $domain)
{
global $woocommerce, $post;
if ($text === 'Related products' && $domain === 'woocommerce') {
$term_names = wp_get_post_terms( $post->ID, 'product_cat', array('fields' => 'names') );
$term_urls = wp_get_post_terms( $post->ID, 'product_cat',
array('fields' => 'slugs') );
$translated = '<a href="#"> </a>'.esc_html__(end($term_urls).'Continue Browising '.end($term_names), $domain);
}
return $translated;
}
2
Answers
This appears to work with out having to copy and modify the /single-product/related.php file. It could be more efficient by using an alternative method.
Using
get_term_link(
from the answer @7uc1f3r providedNormally you could use the
woocommerce_product_related_products_heading
filter hook, which allows you to change$heading
. But$heading
is passed viaesc_html()
so you can’t add HTML to the output.
Therefore you will have to overwrite the /single-product/related.php file
Replace line 29 – 32 @version 3.9.0
With