如何在 WooCommerce 单个产品页面上获取产品属性 link
How to get product attribute link on WooCommerce single product page
我正在使用 WooCommerce,我想获取要在单个产品页面上使用的当前产品属性 URL,将代码 <category_url>/?filter_preco=
替换为 URL。
这是我的代码:
add_action( 'woocommerce_single_product_summary', 'cj_show_attribute_links', 4 );
function cj_show_attribute_links() {
global $post, $product;
$attribute_names = array( 'pa_preco' ); // Insert attribute names here
foreach ( $attribute_names as $attribute_name ) {
$taxonomy = get_taxonomy( $attribute_name );
if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
$terms = wp_get_post_terms( $post->ID, $attribute_name );
$terms_array = array();
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$archive_link = $term->slug;
$base = '<category_url>?filter_preco=';
$full_line = '<h4 style="font-size: 15px; color: #4E4E4E;"><a href="'. $base .'' . $archive_link . '">'. $term->name . '</a></h4><div class="is-divider small"></div>';
array_push( $terms_array, $full_line );
}
echo ' ' . implode( $terms_array);
}
}
}
}
有什么想法吗?
您的代码中有一些错误...使用此替换代码,该代码使用 get_term_link()
WordPress 函数来获取术语 link,如下所示:
add_action( 'woocommerce_single_product_summary', 'display_linked_product_attributes', 4 );
function display_linked_product_attributes() {
global $post, $product;
$taxonomy = array( 'pa_preco' ); // Here define product attribute(s) taxonomy(ies)
foreach ( $attribute_names as $attribute_name ) {
$terms = wp_get_post_terms( get_the_ID(), $taxonomy );
$output = '';
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
$output .= '<h4 style="font-size: 15px; color: #4E4E4E;"><a href="'. $link . '">'. $term->name . '</a></h4><div class="is-divider small"></div>';
}
echo ' ' . $output;
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该更好用。
我用这个得到了解决方案(当产品没有类别时我收到错误)
function pagina_produto_embaixo_titulo() {
global $post;
$attribute_names = array( 'pa_preco' ); // Insert attribute names here
foreach ( $attribute_names as $attribute_name ) {
$taxonomy = get_taxonomy( $attribute_name );
if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
$terms = wp_get_post_terms( $post->ID, $attribute_name );
$terms_array = array();
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$archive_link = $term->slug;
$terms = get_the_terms( $post->ID, 'product_cat' );
$link = get_term_link( $terms[0]->term_id, 'product_cat' );
$base = $link .'?filter_preco=';
//erro nas 2 linhas acima, quando o produto nao tem categoria, e na pagina da loja, deixa de funcuonar algus coisas.
$full_line = '<h4 style="font-size: 15px; color: #4E4E4E;"><a href="https://www.cotacaodebrindes.com.br/o-que-e-ustar/" target="_blank">O que é µStar+?</a> | <a href="'. $base .'' . $archive_link . '">'. $term->name . '</a></h4><div>';
array_push( $terms_array, $full_line );
}
echo ' ' . implode( $terms_array);
}
}
}
}
我正在使用 WooCommerce,我想获取要在单个产品页面上使用的当前产品属性 URL,将代码 <category_url>/?filter_preco=
替换为 URL。
这是我的代码:
add_action( 'woocommerce_single_product_summary', 'cj_show_attribute_links', 4 );
function cj_show_attribute_links() {
global $post, $product;
$attribute_names = array( 'pa_preco' ); // Insert attribute names here
foreach ( $attribute_names as $attribute_name ) {
$taxonomy = get_taxonomy( $attribute_name );
if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
$terms = wp_get_post_terms( $post->ID, $attribute_name );
$terms_array = array();
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$archive_link = $term->slug;
$base = '<category_url>?filter_preco=';
$full_line = '<h4 style="font-size: 15px; color: #4E4E4E;"><a href="'. $base .'' . $archive_link . '">'. $term->name . '</a></h4><div class="is-divider small"></div>';
array_push( $terms_array, $full_line );
}
echo ' ' . implode( $terms_array);
}
}
}
}
有什么想法吗?
您的代码中有一些错误...使用此替换代码,该代码使用 get_term_link()
WordPress 函数来获取术语 link,如下所示:
add_action( 'woocommerce_single_product_summary', 'display_linked_product_attributes', 4 );
function display_linked_product_attributes() {
global $post, $product;
$taxonomy = array( 'pa_preco' ); // Here define product attribute(s) taxonomy(ies)
foreach ( $attribute_names as $attribute_name ) {
$terms = wp_get_post_terms( get_the_ID(), $taxonomy );
$output = '';
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
$output .= '<h4 style="font-size: 15px; color: #4E4E4E;"><a href="'. $link . '">'. $term->name . '</a></h4><div class="is-divider small"></div>';
}
echo ' ' . $output;
}
}
代码进入活动子主题(或活动主题)的 functions.php 文件。它应该更好用。
我用这个得到了解决方案(当产品没有类别时我收到错误)
function pagina_produto_embaixo_titulo() {
global $post;
$attribute_names = array( 'pa_preco' ); // Insert attribute names here
foreach ( $attribute_names as $attribute_name ) {
$taxonomy = get_taxonomy( $attribute_name );
if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
$terms = wp_get_post_terms( $post->ID, $attribute_name );
$terms_array = array();
if ( ! empty( $terms ) ) {
foreach ( $terms as $term ) {
$archive_link = $term->slug;
$terms = get_the_terms( $post->ID, 'product_cat' );
$link = get_term_link( $terms[0]->term_id, 'product_cat' );
$base = $link .'?filter_preco=';
//erro nas 2 linhas acima, quando o produto nao tem categoria, e na pagina da loja, deixa de funcuonar algus coisas.
$full_line = '<h4 style="font-size: 15px; color: #4E4E4E;"><a href="https://www.cotacaodebrindes.com.br/o-que-e-ustar/" target="_blank">O que é µStar+?</a> | <a href="'. $base .'' . $archive_link . '">'. $term->name . '</a></h4><div>';
array_push( $terms_array, $full_line );
}
echo ' ' . implode( $terms_array);
}
}
}
}