将相关URL添加到WooCommerce单产品页面上的相关产品标题

发布于 2025-01-22 15:47:06 字数 1015 浏览 0 评论 0 原文

这将变得更有意义。

如果相关产品标题链接到更多的相关产品...试图更改单个产品页面上的相关产品标题到与相关产品查询相关的最终类别的链接, 被展示。

我有url slug and the End类别的名称,我只是无法逃脱< h2> 标记将其转换为链接。有建议吗?

$ translated ='< a href =“#”> </a>'。esc_html __(end($ term_urls)。'继续浏览'.end'.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;
 
 }

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;
 
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2025-01-29 15:47:06

通常,您可以使用 woocommerce_product_related_products_heading filter Hook,它允许您更改 $ heading 。但是 $ heading 是通过 ESC_HTML()传递的
因此,您无法将HTML添加到输出中。

因此,您将必须覆盖文件

可以通过将其复制到YourTheme/wooCommerce/sillsproduct/ressect.php。

来覆盖此模板。

用29-32 @version 3.9.0

if ( $heading ) :
    ?>
    <h2><?php echo esc_html( $heading ); ?></h2>
<?php endif; ?>

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>';
    }
}
?>

Normally you could use the woocommerce_product_related_products_heading filter hook, which allows you to change $heading. But $heading is passed via esc_html()
so you can't add HTML to the output.

Therefore you will have to overwrite the /single-product/related.php file

This template can be overridden by copying it to yourtheme/woocommerce/single-product/related.php.

Replace line 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>';
    }
}
?>
对你而言 2025-01-29 15:47:06

这似乎与必须复制和修改
/单产品/相关。php文件。通过使用替代方法,它可能会更有效。

使用 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;


}

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 provided

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;


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文