如果相關產品標題鏈接到更多正在顯示的相關產品會更有意義......
嘗試將單個產品頁面上的相關產品標題更改為指向與正在顯示的相關產品查詢相關的最終類別的鏈接。
我有最終類別的 url slug 和名稱,我只是無法轉義<h2>標簽以將其轉換為鏈接。有什么建議嗎?
$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;
}
uj5u.com熱心網友回復:
通常您可以使用woocommerce_product_related_products_heading過濾器掛鉤,它允許您更改$heading. 但是$heading通過,esc_html()
因此您無法將 HTML 添加到輸出中。
因此,您將不得不覆寫/single-product/related.php檔案
可以通過將其復制到 yourtheme/woocommerce/single-product/related.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>';
}
}
?>
uj5u.com熱心網友回復:
這似乎無需復制和修改 /single-product/related.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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/461802.html
標籤:php WordPress woocommerce 产品
