Home > Software design >  WordPress PHP - get_the_term_list() without links
WordPress PHP - get_the_term_list() without links

Time:04-29

I have successfully managed to make a shortcode into my product description, to show all my $taxonomy and $attribute_label values in a nice table form. Unfortunately, it returns the $attribute_label values as a link. I want them to be displayed as text.

$html .= get_the_term_list( $product->get_id(), $taxonomy, '<tr><td>' . $attribute_label . '</td><td style="text-align:right">' , '</td><td>', '</td></tr>' );

I have tried to use wp_get_object_terms but it wont work for me , it returns "Array" in all my Columns in the <table> Form.

My full Code so far:

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 >' . $html . '</table>';
        }
    }

    return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );

I am new to PHP so any help is very appreciated!

CodePudding user response:

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 >' . $html . '</table>';
        }
    }

    return $html;
}

This will print like enter image description here

Tested with a sample attribute Color

  • Related