Home > database >  How to output foreach inside echo
How to output foreach inside echo

Time:08-19

I need to output inside echo foreach to output the terms that belong to this post. How can i do this? The code I wrote is not working.

$cur_terms = get_the_terms( $ajaxposts->post->ID, 'cities' ); 

 echo '<div >
        '. foreach( $cur_terms as $cur_term ): .'
         <p>
            '. echo $cur_term->name .'      
         </p>
         '.endforeach; .' 
       </div>';

CodePudding user response:

You have to use foreach block and print inside the block

$cur_terms = get_the_terms( $ajaxposts->post->ID, 'cities' ); 

echo '<div >';
foreach( $cur_terms as $cur_term )
{
    echo'<p>'. $cur_term->name .'</p>';
}
echo'</div>';

CodePudding user response:

You will want to echo separately. And echo within the foreach loop.

$cur_terms = get_the_terms( $ajaxposts->post->ID, 'cities' ); 

echo '<div >';
foreach( $cur_terms as $cur_term ){
    echo '<p>{$cur_term->name}</p>';
}
echo '</div>';
  • Related