admin管理员组

文章数量:1415476

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I'm trying to get all atribute names and images(thumbnails?) to print them , I've managed to get their names , but not the url of the image I can't find it . To add an image to woocommerce attributes I used the Variation swatches extension .

Here is how I get the names of the attributes

$attributes = get_terms("pa_couleurs");
foreach ($attributes as $attribute){
    Print  $attribute->name;
}

Ideally , I want to get the image URL so I can put them inside an html tag .

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I'm trying to get all atribute names and images(thumbnails?) to print them , I've managed to get their names , but not the url of the image I can't find it . To add an image to woocommerce attributes I used the Variation swatches extension .

Here is how I get the names of the attributes

$attributes = get_terms("pa_couleurs");
foreach ($attributes as $attribute){
    Print  $attribute->name;
}

Ideally , I want to get the image URL so I can put them inside an html tag .

Share Improve this question asked Sep 8, 2019 at 18:49 HazaHaza 213 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

There is a lovely function get_term_link(), which you can use to get the any WP_Term object archive page URL.

Just add this inside your foreach loop:

$url = get_term_link( $attribute );

Edit

Since you need to retrieve a certain meta value for each attribute, try fetching them all and var_dumping them, to see what is it exactly that you need.

For example:

/** @var \WP_Term[] $attributes */
$attributes = get_terms( 'pa_couleurs' );

foreach ( $attributes as $attribute ) {
    echo  $attribute->name;

    // Get all attribute meta data
    $meta = get_term_meta( $attribute->term_id );

    // Dump it out on the page. Remove after you find the key(s) you need
    echo '<pre>';
    print_r( $meta );
    echo '</pre>';

    // After finding the exact meta key that holds the info you need, edit this
    $images = (array) get_term_meta( $attribute->term_id, 'change_this', true );

    // This will probably hold the array of attachment IDs, so you'll need to get the URL's from that
    foreach ( $images as $image_id ) {

        // Get image src from ID
        $src = wp_get_attachment_image_url( $image_id, 'thumbnail' );

        // If src is found
        if ( $src ) {
            echo '<img src="' . $src . '" alt="Attribute Image">';

        } // If not
        else {
            echo 'No image src for the image ID ' . $image_id . '<br>';
        }
    }
}

本文标签: woocommerce offtopicHow to get woocomerce attributes thumbnail in PHP