Woocommerce - 在前端显示带有短代码的单个产品属性 [英] Woocommerce - Display single product attribute(s) with shortcodes in Frontend
问题描述
过去几天我在这里阅读了很多 Q/A,但不幸的是,没有一个能解决我的问题.
我正在尝试获取产品属性并使用短代码在前端显示它们.我已经设法显示所有可用的属性并将它们显示在列表中,但我只需要在不同的位置选择其中的一两个(这就是使用简码的原因).例如像 [shortcode_attribute name="brand"].
非常感谢任何帮助!
到目前为止我的代码:
function tutsplus_list_attributes( $product ) {全球$产品;全球 $post;$attributes = $product->get_attributes();如果(!$属性){返回;}foreach ( $attributes 作为 $attribute ) {//获取分类.$terms = wp_get_post_terms( $product->id, $attribute[ 'name' ], 'all' );$taxonomy = $terms[0]->taxonomy;//获取分类对象.$taxonomy_object = get_taxonomy( $taxonomy );//获取属性标签.$attribute_label = $taxonomy_object->labels->name;//显示标签后跟一个可点击的术语列表.echo get_the_term_list( $post->ID, $attribute[ 'name' ] , '<div><li class="bullet-arrow">' . $attribute_label . ': ' , ', ', '</li>
' );}}add_action('woocommerce_product_meta_end', 'tutsplus_list_attributes');add_shortcode('display_attributes', 'tutsplus_list_attributes');
虽然我仍然不是 100% 清楚你在追求什么,但我要创建的是一个短代码,它创建了所有术语的列表属性.为了使其更加灵活,我将添加对简码参数的支持,以便您可以创建特定属性术语的列表.
您需要从代码中更改的一项主要内容是,短代码需要返回一个字符串而不是 ECHO 输出 html.回显短代码可能会导致意想不到的怪异.
/*** 属性简码回调.*/函数 so_39394127_attributes_shortcode( $atts ) {全球$产品;if( !is_object( $product ) || ! $product->has_attributes() ){返回;}//解析短代码属性$args = shortcode_atts( 数组('属性' =>array_keys( $product->get_attributes() ),//默认显示所有属性), $atts );//传递一个属性参数,转成数组if( is_string( $args['attributes'] ) ){$args['attributes'] = array_map('trim',explode('|', $args['attributes']));}//以空字符串开头,因为短代码需要返回而不是回显值$html = '';if( !empty( $args['attributes'] ) ){foreach ( $args['attributes'] 作为 $attribute ) {//获取 WC 标准的属性分类名称$taxonomy = strpos( $attribute, 'pa_' ) === false ?wc_attribute_taxonomy_name( $attribute ) : $attribute;if( taxonomy_is_product_attribute( $taxonomy ) ){//获取属性标签.$attribute_label = wc_attribute_label( $taxonomy );//使用标签后跟可点击的术语列表构建 html 字符串.//为 WC3.0 更新以使用 getter 而不是直接访问属性.$html .= get_the_term_list( $product->get_id(), $taxonomy, '<li class="bullet-arrow">' . $attribute_label . ': ' , ', ', '</li>');}}//如果我们有任何东西要显示,把它包裹在一个 <ul>适当的标记//或者:如果您只想返回 <li>,请删除这些行元素如果( $html ){$html = ''.$html .'</ul>';}}返回 $html;}add_shortcode('display_attributes', 'so_39394127_attributes_shortcode');
用法:这将用于两种方式中的一种.
首先,要显示特定的属性使用:
[display_attributes attributes="color|material"]
二、显示所有属性使用:
[display_attributes]
编辑
这是一个始终单一显示的版本:
/*** 属性简码回调.*/函数 so_39394127_singular_attribute_shortcode( $atts ) {全球$产品;if( !is_object( $product ) || ! $product->has_attributes() ){返回;}//解析短代码属性$args = shortcode_atts( 数组('属性' =>''), $atts );//以空字符串开头,因为短代码需要返回而不是回显值$html = '';if( $args['attribute'] ){//获取 WC 标准的属性分类名称$taxonomy = strpos( $args['attribute'], 'pa_' ) === false ?wc_attribute_taxonomy_name( $args['attribute'] ) : $args['attribute'];if( taxonomy_is_product_attribute( $taxonomy ) ){//获取属性标签.$attribute_label = wc_attribute_label( $taxonomy );//使用标签后跟可点击的术语列表构建 html 字符串.//为 WC3.0 更新以使用 getter 而不是直接访问属性.$html .= get_the_term_list( $product->get_id(), $taxonomy, $attribute_label . ': ' , ', ', '' );}}返回 $html;}add_shortcode('display_attribute', 'so_39394127_singular_attribute_shortcode');
这会删除所有 HTML 标记,因此您需要提供自己的...如果您正在制作自定义列表,就会发生这种情况.
<li class="arrow">静态列表项</li>- [display_attribute 属性="颜色"]
<ul>
编辑:此代码可以添加到您主题的functions.php
,但最好添加到站点特定插件 或通过代码片段 插件.
i've read many Q/A's here during the last few days, but unfortunately none of them solved my issue.
I'm trying to fetch product attributes and display them on the frontend with a shortcode. I have managed to display ALL available attributes and display them in a list, but i need to select only one or two of them in different locations (thats why using shortcodes). For example like [shortcode_attribute name="brand"].
Any help is highly appreciated!
my code so far:
function tutsplus_list_attributes( $product ) {
global $product;
global $post;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
foreach ( $attributes as $attribute ) {
// Get the taxonomy.
$terms = wp_get_post_terms( $product->id, $attribute[ 'name' ], 'all' );
$taxonomy = $terms[ 0 ]->taxonomy;
// Get the taxonomy object.
$taxonomy_object = get_taxonomy( $taxonomy );
// Get the attribute label.
$attribute_label = $taxonomy_object->labels->name;
// Display the label followed by a clickable list of terms.
echo get_the_term_list( $post->ID, $attribute[ 'name' ] , '<div><li class="bullet-arrow">' . $attribute_label . ': ' , ', ', '</li></div>' );
}
}
add_action( 'woocommerce_product_meta_end', 'tutsplus_list_attributes' );
add_shortcode('display_attributes', 'tutsplus_list_attributes');
While I am still not 100% clear what you're after, what I'm going to create is a shortcode that creates a list of all terms in all attributes. To make it more flexible I'll add support for a shortcode parameter so you can create a list of specific attribute terms.
One major thing you need to alter from your code, is that shortcodes need to RETURN a string and not ECHO out html. Echoing out shortcodes can result in unexpected weirdness.
/**
* Attributes shortcode callback.
*/
function so_39394127_attributes_shortcode( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attributes' => array_keys( $product->get_attributes() ), // by default show all attributes
), $atts );
// is pass an attributes param, turn into array
if( is_string( $args['attributes'] ) ){
$args['attributes'] = array_map( 'trim', explode( '|' , $args['attributes'] ) );
}
// start with a null string because shortcodes need to return not echo a value
$html = '';
if( ! empty( $args['attributes'] ) ){
foreach ( $args['attributes'] as $attribute ) {
// get the WC-standard attribute taxonomy name
$taxonomy = strpos( $attribute, 'pa_' ) === false ? wc_attribute_taxonomy_name( $attribute ) : $attribute;
if( taxonomy_is_product_attribute( $taxonomy ) ){
// Get the attribute label.
$attribute_label = wc_attribute_label( $taxonomy );
// Build the html string with the label followed by a clickable list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= get_the_term_list( $product->get_id(), $taxonomy, '<li class="bullet-arrow">' . $attribute_label . ': ' , ', ', '</li>' );
}
}
// if we have anything to display, wrap it in a <ul> for proper markup
// OR: delete these lines if you only wish to return the <li> elements
if( $html ){
$html = '<ul class="product-attributes">' . $html . '</ul>';
}
}
return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );
Usage: This would be used in 1 of 2 ways.
First, to display specific attributes use:
[display_attributes attributes="color|material"]
Second, to display all attributes use:
[display_attributes]
Edit
Here's an always single display edition:
/**
* Attribute shortcode callback.
*/
function so_39394127_singular_attribute_shortcode( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attribute' => ''
), $atts );
// start with a null string because shortcodes need to return not echo a value
$html = '';
if( $args['attribute'] ){
// get the WC-standard attribute taxonomy name
$taxonomy = strpos( $args['attribute'], 'pa_' ) === false ? wc_attribute_taxonomy_name( $args['attribute'] ) : $args['attribute'];
if( taxonomy_is_product_attribute( $taxonomy ) ){
// Get the attribute label.
$attribute_label = wc_attribute_label( $taxonomy );
// Build the html string with the label followed by a clickable list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= get_the_term_list( $product->get_id(), $taxonomy, $attribute_label . ': ' , ', ', '' );
}
}
return $html;
}
add_shortcode( 'display_attribute', 'so_39394127_singular_attribute_shortcode' );
This removes all HTML markup, and so you'd need to provide your own... which would happen if you are making a custom list.
<ul>
<li class="arrow">Static List Item</li>
<li class="arrow">[display_attribute attribute="color"]</li>
<ul>
EDIT: This code can be added to your theme's functions.php
, but preferably either in a site-specific plugin or via the Code Snippets plugin.
这篇关于Woocommerce - 在前端显示带有短代码的单个产品属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!