GTO*_*GTO 2 attributes woocommerce
在过去的几天里,我在这里阅读了许多 Q/A,但不幸的是,它们都没有解决我的问题。
我正在尝试获取产品属性并使用短代码在前端显示它们。我已经设法显示所有可用的属性并将它们显示在列表中,但我只需要在不同的位置选择其中的一两个(这就是使用简码的原因)。例如像 [shortcode_attribute name="brand"]。
任何帮助表示高度赞赏!
到目前为止我的代码:
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');
Run Code Online (Sandbox Code Playgroud)
hel*_*ing 11
虽然我仍然不是 100% 清楚您在追求什么,但我要创建的是一个短代码,用于创建所有属性中所有术语的列表。为了使其更加灵活,我将添加对简码参数的支持,以便您可以创建特定属性术语的列表。
您需要从代码中更改的一件主要事情是,短代码需要返回一个字符串而不是 ECHO 输出 html。回显短代码可能会导致意想不到的怪异。
/**
* 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' );
Run Code Online (Sandbox Code Playgroud)
用法:这将以两种方式之一使用。
首先,要显示特定属性,请使用:
[display_attributes attributes="color|material"]
Run Code Online (Sandbox Code Playgroud)
其次,要显示所有属性,请使用:
[display_attributes]
Run Code Online (Sandbox Code Playgroud)
编辑
这是一个始终单一显示的版本:
/**
* 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' );
Run Code Online (Sandbox Code Playgroud)
这会删除所有 HTML 标记,因此您需要提供自己的...如果您正在制作自定义列表,就会发生这种情况。
<ul>
<li class="arrow">Static List Item</li>
<li class="arrow">[display_attribute attribute="color"]</li>
<ul>
Run Code Online (Sandbox Code Playgroud)
编辑:此代码可以添加到您的主题中functions.php,但最好是在特定于站点的插件中或通过代码片段插件。
| 归档时间: |
|
| 查看次数: |
13632 次 |
| 最近记录: |