WordPress PHP - get_the_term_list() 无链接
WordPress PHP - get_the_term_list() without links
我已经成功地在我的产品描述中制作了一个短代码,以漂亮的 table 形式显示我所有的 $taxonomy
和 $attribute_label
值。不幸的是,它 returns 的 $attribute_label
值为 link。我希望它们显示为文本。
$html .= get_the_term_list( $product->get_id(), $taxonomy, '<tr><td>' . $attribute_label . '</td><td style="text-align:right">' , '</td><td>', '</td></tr>' );
我曾尝试使用 wp_get_object_terms
,但它对我不起作用,它 returns 在我的 <table>
表格中的所有专栏中“排列”。
到目前为止我的完整代码:
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 list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= wp_get_object_terms( $product->get_id(), $taxonomy, '<tr><td>' . $attribute_label . '</td><td style="text-align:right">' , '</td><td>', '</td></tr>' );
}
}
// if we have anything to display, wrap it in a <table> for proper markup
if( $html ){
$html = '<table class="product-attributes">' . $html . '</table>';
}
}
return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );
我是 PHP 的新手,非常感谢您的帮助!
function ts_add_text_short_descr($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 list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= "<tr><td>$attribute_label</td>";
$terms_list = wp_get_object_terms($product->get_id(), $taxonomy);
foreach ($terms_list as $term) {
$html .= '<td style="text-align:right">' . $term->name . '</td>';
}
$html .= '</tr>';
}
}
// if we have anything to display, wrap it in a <table> for proper markup
if ($html) {
$html = '<table class="product-attributes">' . $html . '</table>';
}
}
return $html;
}
这将打印为
使用示例属性颜色进行测试
我已经成功地在我的产品描述中制作了一个短代码,以漂亮的 table 形式显示我所有的 $taxonomy
和 $attribute_label
值。不幸的是,它 returns 的 $attribute_label
值为 link。我希望它们显示为文本。
$html .= get_the_term_list( $product->get_id(), $taxonomy, '<tr><td>' . $attribute_label . '</td><td style="text-align:right">' , '</td><td>', '</td></tr>' );
我曾尝试使用 wp_get_object_terms
,但它对我不起作用,它 returns 在我的 <table>
表格中的所有专栏中“排列”。
到目前为止我的完整代码:
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 list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= wp_get_object_terms( $product->get_id(), $taxonomy, '<tr><td>' . $attribute_label . '</td><td style="text-align:right">' , '</td><td>', '</td></tr>' );
}
}
// if we have anything to display, wrap it in a <table> for proper markup
if( $html ){
$html = '<table class="product-attributes">' . $html . '</table>';
}
}
return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );
我是 PHP 的新手,非常感谢您的帮助!
function ts_add_text_short_descr($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 list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= "<tr><td>$attribute_label</td>";
$terms_list = wp_get_object_terms($product->get_id(), $taxonomy);
foreach ($terms_list as $term) {
$html .= '<td style="text-align:right">' . $term->name . '</td>';
}
$html .= '</tr>';
}
}
// if we have anything to display, wrap it in a <table> for proper markup
if ($html) {
$html = '<table class="product-attributes">' . $html . '</table>';
}
}
return $html;
}
这将打印为
使用示例属性颜色进行测试