PHP - 简单 table
PHP - Simple table
如何在这样的 table 中显示我的代码输出?
我想以 table 格式而不是列表显示 $attribute
和 $attribute_label
的标签和值。
我是 PHP 的新手,不知道该怎么做。我希望有人能帮助我解决我的问题。
// 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 .= wp_get_object_terms( $product->get_id(), $taxonomy, '<li>' . $attribute_label . ': ' , ', ', '</li>' );
到目前为止我的代码:
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 .= wp_get_object_terms( $product->get_id(), $taxonomy, '<li>' . $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' );
您需要更换:
ul
来自 table
li
通过 tr
和 td
所以你应该替换这个:
// 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 .= wp_get_object_terms( $product->get_id(), $taxonomy, '<li>' .
$attribute_label . ': ' , ', ', '</li>' );
作者:
$html .= wp_get_object_terms( $product->get_id(), $taxonomy, '<tr><td>' .
$attribute_label . '</td><td>' , '</td><td>' , '</td></tr>' );
而对于 ul
:
$html = '<ul class="product-attributes">' . $html . '</ul>';
作者:
$html = '<table class="product-attributes">' . $html . '</table>';
如何在这样的 table 中显示我的代码输出?
我想以 table 格式而不是列表显示 $attribute
和 $attribute_label
的标签和值。
我是 PHP 的新手,不知道该怎么做。我希望有人能帮助我解决我的问题。
// 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 .= wp_get_object_terms( $product->get_id(), $taxonomy, '<li>' . $attribute_label . ': ' , ', ', '</li>' );
到目前为止我的代码:
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 .= wp_get_object_terms( $product->get_id(), $taxonomy, '<li>' . $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' );
您需要更换:
ul
来自table
li
通过tr
和td
所以你应该替换这个:
// 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 .= wp_get_object_terms( $product->get_id(), $taxonomy, '<li>' .
$attribute_label . ': ' , ', ', '</li>' );
作者:
$html .= wp_get_object_terms( $product->get_id(), $taxonomy, '<tr><td>' .
$attribute_label . '</td><td>' , '</td><td>' , '</td></tr>' );
而对于 ul
:
$html = '<ul class="product-attributes">' . $html . '</ul>';
作者:
$html = '<table class="product-attributes">' . $html . '</table>';