简体   繁体   中英

How do I create a while loop within a PHP string?

I am creating a query using shortcodes ultimate lightbox. But the only way this will work within a regular php page, is to save the data as string. So what I need to do is to create my query but somehow get my results within a string.

Here is what works before I use any kind of php query:

 <?php     
 $my_tabs = "<ul class='easybuttons'>
 <li>[su_lightbox type='inline' src='#lightbox1']AT&amp;T[/su_lightbox]</li>
 <li>[su_lightbox type='inline' src='#lightbox2']Sprint[/su_lightbox]</li>
 <li>[su_lightbox type='inline' src='#lightbox3']T-Mobile[/su_lightbox]</li>
 </ul>";

 echo do_shortcode( $my_tabs );
 ?> 

but I need the ATT, Sprint, T-Mobile to be dynamic. Keep in mind the shortcode will only work if it within a string.

So how can I do a while loop within this string? I tried using an operator but did not work.

$args = array('post_type' => 'services', 'category_name' =>  $childid, 'order_by' => 'the_title', 'order' => 'ASC');     

 query_posts($args);

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
 $my_tabs .= '<ul class="easybuttons">'; 
 while ( $the_query->have_posts() ) { 
 $the_query->the_post();
 $my_tabs .= '<li>[su_lightbox type="inline" src="#lightbox1"]' . get_the_title() . '</li>';
 }
$my_tabs .= '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata();

  echo do_shortcode( $my_tabs );
 ?>

UPDATE:

I tried using this code but it does work. Nothing comes through. I don't get any errors but no shortcode is displayed.

 <?php 
  $args = array('post_type' => 'services', 'category_name' =>  $childid, 'order_by' => 'the_title', 'order' => 'ASC');   


 // The Query
  $the_query = new WP_Query( $args );

 // The Loop
 if ( $the_query->have_posts() ) {
  $lid = 1;
  $my_tabs .= '<ul class="easybuttons">'; 
  while ( $the_query->have_posts() ) { 
   $the_query->the_post();
   $my_tabs .= '<li>[su_lightbox type="inline" src="#lightbox' . $lid . '"]' . get_the_title() . '</li>';
   $lid++;
  }
 $my_tabs .= '</ul>';
 }

 echo do_shortcode( $my_tabs );
 wp_reset_postdata();   

You need to initialise the variable $my_tabs somewhere, for instance outside the if block, and increment the lightbox id. You don't need to call query_posts() . order_by should be title , not the_title . Make sure $childid is definitely a string of the category slug , not the name, if in doubt, take out that parameter altogether to see if you get anything as I imagine this is most likely to be the main issue.

$args = array('post_type' => 'services', 'category_name' =>  $childid, 'order_by' => 'title', 'order' => 'ASC');

// The Query
$the_query = new WP_Query( $args );

$my_tabs = '';

// The Loop
if ( $the_query->have_posts() ) {
 $lid = 1;
 $my_tabs .= '<ul class="easybuttons">'; 
 while ( $the_query->have_posts() ) { 
  $the_query->the_post();
  $my_tabs .= '<li>[su_lightbox type="inline" src="#lightbox' . $lid . '"]' . get_the_title() . '</li>';
  $lid++;
 }
$my_tabs .= '</ul>';
}

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