简体   繁体   中英

display the terms of a product attribute in a specific page

Can I display all the terms of a woocommerce product attribute in a specific page (even empty terms) and in front of each term the number of products linked with it?

Example:

Attribute A

Term 1 (2) Term 2 (1) Term 3 (0) Term 4 (2) Term 5 (1) Term 6 (0)

Regards

I like to have a short code, a small php code or even a plugin

Usage example [listtaxonomyterms attribute="pa_color"]

add_shortcode('listtaxonomyterms', 'list_taxonomy_terms');
function list_taxonomy_terms($attr) {
    $args = shortcode_atts( array(
        'attribute' => '', // example pa_color 
    ), $attr );

    if(!$args['attribute']) return; // If there is no attribute

    $terms = get_terms( array(
        'taxonomy' => $args['attribute'],
        'hide_empty' => false, // set to true to exclude empty
    ) );
    $attributes = array();
    if($terms):
        foreach($terms as $term):
            /* What we will get from that
            [term_id][name][slug][term_group][term_taxonomy_id][taxonomy][description][parent][count][filter]
            */
            $attributes[] = '<div><span class="name">'.$term->name.'</span><span class="count">'.$term->count.'</span></div>';
        endforeach;
    endif;

    return sprintf('<div class="attribute-list">%s</div>',implode( '', $attributes ));
}

thank you for your feedback.

I tried the shortcode you sent me and it doesn't work.

I have already used the product filter block by attribute in a custom page and it works except for 2 things: a- Empty terms are not displayed. b- there is not in front of each term the number of the linked product.

I also used attribute display with the "Product Filter" block and it's the same as the "Product Filter by Attribute" block, empty terms are not displayed and there is no in front of each term the linked product number, except for the display of categories.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM