Slider in theme template using shortcode
-
Hi,
I’m building a slider into a theme template using shortcode, as you suggested in a different thread a year ago, using something like this:
<?php $my_slider = '[slider]'; $my_slider .= '<p>test 1</p>'; $my_slider .= '[next-slide]'; $my_slider .= '<p>test 2</p>'; $my_slider .= '[/slider]'; echo do_shortcode( $my_slider ); ?>
I’m building this inside of a loop, so that each slide is made of one custom-post-type item:
<?php if ( $our_query->have_posts() ) : ?> <?php function load_template_part($template_name, $part_name=null) { ob_start(); get_template_part($template_name, $part_name); $var = ob_get_contents(); ob_end_clean(); return $var; } ?> <?php $my_slider = '[slider]'; $my_slider .= '<div><p>THIS IS THE PROBLEM RIGHT HERE.</p></div>'; ?> <?php // The loop while ( $our_query->have_posts() ) : $our_query->the_post(); ?> <?php $my_slider .= '[next-slide]'; $my_slider .= load_template_part( 'content', 'homebox' ); ?> <?php endwhile; // end of the loop ?> <?php wp_reset_postdata(); ?> <?php $my_slider .= '[/slider]'; echo do_shortcode( $my_slider ); ?>
This would work fine, except that in the plugin’s shortcode structure, the first slide must be created with different code than all of the remaining slides. (The code for the first slide follows directly after the opening
[slider]
shortcode, while the remaining slides each follow a[next-slide]
shortcode.) This keeps me from setting it up programmatically, one loop-item per slide.So far, the only solution I’ve come up with is to put some “filler” content into the first slide, an ugly workaround. Can you think of another approach?
- The topic ‘Slider in theme template using shortcode’ is closed to new replies.