[英]Add relative URL to related products title on WooCommerce single product page
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.我有 url slug 和结束类别的名称我只是无法转义
<h2>
标记以将其转换为链接。 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;
}
Normally you could use the woocommerce_product_related_products_heading
filter hook, which allows you to change $heading
.通常您可以使用
woocommerce_product_related_products_heading
过滤器挂钩,它允许您更改$heading
。 But $heading
is passed via esc_html()
so you can't add HTML to the output.但是
$heading
是通过esc_html()
传递的,所以你不能将 HTML 添加到 output。
Therefore you will have to overwrite the /single-product/related.php file因此,您必须覆盖/single-product/related.php文件
This template can be overridden by copying it to yourtheme/woocommerce/single-product/related.php.
可以通过将此模板复制到 yourtheme/woocommerce/single-product/related.php 来覆盖此模板。
Replace line 29 - 32 @version 3.9.0替换第 29 - 32 行 @version 3.9.0
if ( $heading ) :
?>
<h2><?php echo esc_html( $heading ); ?></h2>
<?php endif; ?>
With和
if ( $heading ) {
global $product;
// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Get terms
$terms = wp_get_post_terms( $product->get_id(), 'product_cat' );
$end = end( $terms );
// URL
echo '<a href="' . get_term_link( $end->term_id, 'product_cat' ) . '">' . $end->name . '</a>';
}
}
?>
This appears to work with out having to copy and modify the /single-product/related.php file.这似乎无需复制和修改/single-product/related.php文件即可工作。 It could be more efficient by using an alternative method.
使用替代方法可能会更有效。
Using get_term_link(
from the answer @7uc1f3r provided使用
get_term_link(
来自 @7uc1f3r 提供的答案
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') );
$term_ids = wp_get_post_terms( $post->ID, 'product_cat',
array('fields' => 'ids') );
$last_term_id = end($term_ids);
$translated = _e('<h2><a href="' . get_term_link( $last_term_id, 'product_cat' ) . '">More ' . end($term_names) . '</a></h2>', $domain);
}
return $translated;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.