Search code examples
phpwordpressfunctionforeachshortcode

Return inside foreach inside shortcode


I'm using the PHP code below to display CSS classes based on the custom taxonomies applied to my WordPress posts. My taxonomy is called CC and its three options are x, y, and z. For a post that has all three:

 $cc_terms = get_the_terms($post->ID,'cc');
 foreach ($cc_terms as $term) { 
     echo ' '.$term->slug.'-active'; 
 }

outputs: x-active y-active z-active

I'm trying to convert it into WordPress shortcode function (where echo doesn't work as far as I know) and basically I need to figure out how to capture cc_class() as one variable (not an array) so that it give me the same output as above.

function cc_meta($atts, $content = null) {
    global $post;
    extract(shortcode_atts(array('class' => 'default'), $atts));
    function cc_class() {
        $cc_terms = get_the_terms($post->ID, 'cc');
        foreach($cc_terms as $term) {
            return ' '.$term->slug.'-active';
        }
    }
    return '<div class="' . esc_attr($class) . cc_class() . '">...</div>';
}

outputs: <div class="default x-active">...</div>

It's only taking the first term rather than all three, which makes me wonder how return works in foreach loops. Any idea how I can get this to work?

desired output: <div class="default x-active y-active z-active">...</div>

Update 1: I tried with the .= as suggested and I'm getting a blank error:

function cc_meta( $atts, $content = null ) {
    global $post;
    extract(shortcode_atts(array('class' => 'default'), $atts));

    $cc_terms = get_the_terms($post->ID, 'cc');
    foreach ($cc_terms as $term) {
        $return .= ' '.$term->slug.'-active';
    }
    return '<div class="' . esc_attr($class) . $return . '">...</div>';
}

Update 2: I removed the global $post line and it works—sweet! Thanks everyone =)

function cc_meta($atts, $content = null) {
    extract(shortcode_atts(array('class' => 'default'), $atts));
    $cc_terms = get_the_terms($post->ID, 'cc');
    foreach($cc_terms as $term) { 
        $return .= ' '.$term->slug.'-active';
    }
    return '<div class="' . esc_attr($class) . $return . '">...</div>';
}

Solution

  • Don't return in the middle of your function, use return only at the end.

    Instead of return use this:

    $return .= 
    

    and before the closing } do a return $return;