简体   繁体   中英

How to insert html with php into a variable?

I need to write my html into the $response variable, in which I dynamically display posts. How can i do this? My code to write to a variable:

 <ul class="speakers-list"> <?php $args = array( 'post_type' => 'speakers', ); $loop = new WP_Query( $args ); while ($loop->have_posts()): $loop->the_post(); ?> <li> <a href="<?php echo esc_attr(the_permalink())?>"> <div class="speaker-img"> <?php $img_url = get_the_post_thumbnail_url( $loop->post->ID ); ?> <img src="<?php echo $img_url?>"> </div> <div class="speaker-name"> <p> <?php the_title(); ?> </p> </div> <div class="speaker-city"> <p> Fribourg </p> </div> </a> </li> <?php endwhile; wp_reset_query(); ?> </ul>

Variable to write html:

 if($ajaxposts->have_posts()) { while($ajaxposts->have_posts()): $ajaxposts->the_post(); $response 'HERE TO ADD HTML' endwhile; } else { $response = 'empty'; } echo $response; exit;

Looks like a bug to me. There is an equal sign missing between $response and 'HERE TO ADD HTML'.

By using the = sign between the variable and the value, you effectively set the variable on the left to be whatever is on the right. Then you will be able to reference it the normal way using $response

 if($ajaxposts->have_posts()) { while($ajaxposts->have_posts()): $ajaxposts->the_post(); /* BUG RIGHT HERE IS PREVENTING YOUR CODE FROM RUNNING */ /* $response 'HERE TO ADD HTML' */ /* BUG FIX */ $response = 'HERE TO ADD HTML'; endwhile; } else { $response = 'empty'; } echo $response; exit;

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