-2

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;
userName
  • 903
  • 2
  • 20

1 Answers1

0

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;
mike510a
  • 2,102
  • 1
  • 11
  • 27