4

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

1 Answer 1

5

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>';
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yea I do! That was actually my next challenge! I will try this!
This new query comes up with no results. Hmm
@user2593040 See my updated answer. If it still doesn't work, try var_dump($the_query), to see what comes out
@user2593040 And have you tried removing 'category_name' => $childid from the arguments? If so, could you post that vardump? Something in the query is resulting in no posts being displayed, and this is my best guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.