简体   繁体   English

在WooCommerce单品页的相关商品标题添加URL

[英]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.

相关问题 在单个产品页面上增加相关产品(woocommerce) - Increase related products on single product page (woocommerce) WooCommerce:仅在单个产品页面上显示价格后缀 - 不适用于相关产品 - WooCommerce: Show price suffix only on single product Page - Not for related products 在 WooCommerce 单个产品页面上仅显示“有货”相关产品 - Show only "in stock" related products on WooCommerce single product page WooCommerce单品页面隐藏“缺货”相关产品 - Hide “out of stock” related products on WooCommerce single product page 在 Woocommerce 单个产品的标题后添加特定的产品属性 - Add specific product attribute after title on Woocommerce single products 在 Woocommerce 单个产品的标题前添加链接的特定产品属性 - Add linked specific product attribute before title on Woocommerce single products 将相关商品移至Woocommerce单品侧边栏 - Move related product to the sidebar on Woocommerce single products Woocommerce - 仅当有加售产品时才将 html 添加到单个产品页面 - Woocommerce - Add html to the single product page only if there are upsell products 在 WooCommerce 的单个产品页面上为低库存产品添加 CSS 类 - Add CSS class for low stock products on single product page in WooCommerce 产品页面在woocommerce单个产品页面中没有显示任何产品 - product page shows no products in woocommerce single product page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM