繁体   English   中英

如何将带有 php 的 html 插入到变量中?

[英]How to insert html with php into a variable?

我需要将我的 html 写入 $response 变量,在其中动态显示帖子。 我怎样才能做到这一点? 我写入变量的代码:

 <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>

写入 html 的变量:

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

对我来说似乎是一个错误。 $response 和“这里添加 HTML”之间缺少等号。

通过在变量和值之间使用 = 符号,您可以有效地将左侧的变量设置为右侧的变量。 然后您将能够使用$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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM