dimanche 19 avril 2015

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();

Aucun commentaire:

Enregistrer un commentaire