Search code examples
phparrayswordpresswoocommerceimplode

Can't set a coma separator using implode() on an array in WordPress


Can't set the correct separator for array conversion to string. I am using

echo .implode(  ",", $terms_array ).;

The output is: array1array2array3 instead of array1,array2,array3

In sandbox it works fine, but when I put code to WordPress, it's not working.

What can be a reason?

function wc_show_attribute_links_prodcart() {
    
    global $post;
    $attribute_names = array( 'pa_attr1', 'pa_attr2','pa_attr3' ); 
        
    foreach ( $attribute_names as $attribute_name ) {
        $taxonomy = get_taxonomy( $attribute_name );
        
        if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
            $terms = wp_get_post_terms( $post->ID, $attribute_name );
            $terms_array = array();
        
            if ( ! empty( $terms ) ) {
                foreach ( $terms as $term ) {
                   $archive_link = get_term_link( $term->slug, $attribute_name );
                   $full_line = '<a href="' . $archive_link . '">'. $term->name . '</a>';
                   array_push( $terms_array, $full_line );
                }
                //echo .implode(  ",", $terms_array );
             echo implode( ',', $terms_array );
            }
        }
    }
}

Solution

  • You are iterating your three $attribute_names.

    If $taxonomy is truthy, you are clearing out your $terms_array each time.

    You are imploding and printing at the end of each outer loop, so there will be no comma between what you are calling array1 ...etc.

    To better visualize the output, use a linebreak <br> at the end of your echo.

    Or if you want all hyperlinks to be comma-separated, just declare a master array to collect them all then implode when you are done.

    function wc_show_attribute_links_prodcart() {
        global $post;
        $hyperlinks = [];
    
        foreach (['pa_attr1', 'pa_attr2', 'pa_attr3'] as $attribute_name) {
            $taxonomy = get_taxonomy($attribute_name);
            if ($taxonomy && !is_wp_error($taxonomy)) {
                foreach (wp_get_post_terms($post->ID, $attribute_name) as $term) {
                    $archive_link = get_term_link($term->slug, $attribute_name);
                    $hyperlinks[] = '<a href="' . $archive_link . '">'. $term->name . '</a>';
                }
            }
        }
        echo implode(',', $hyperlinks);
    }
    

    Or with all hyperlinks separated per group and groups visibly separated:

    function wc_show_attribute_links_prodcart() {
        global $post;
        $all_term_links = [];
    
        foreach (['pa_attr1', 'pa_attr2', 'pa_attr3'] as $attribute_name) {
            $taxonomy = get_taxonomy($attribute_name);
            if ($taxonomy && !is_wp_error($taxonomy)) {
                $term_links = [];
                foreach (wp_get_post_terms($post->ID, $attribute_name) as $term) {
                    $archive_link = get_term_link($term->slug, $attribute_name);
                    $term_links[] = '<a href="' . $archive_link . '">'. $term->name . '</a>';
                }
                $all_term_links[] = implode(' & ', $term_links);
            }
        }
        echo implode(',', $all_term_links);
    }
    

    https://developer.wordpress.org/reference/functions/wp_get_post_terms/ wp_get_post_terms() returns an array so it can be fed directly to the foreach().