• Resolved hommealone

    (@hommealone)


    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?

    https://www.ads-software.com/plugins/bxslider-integration/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter hommealone

    (@hommealone)

    Well, I solved this myself.

    I’ve used a counter to increment each post and output only the first one differently.

    <?php
    // counter to differentiate first item...
    $inc = 1;
    ?>
    <?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 // The loop
    while ( $our_query->have_posts() ) : $our_query->the_post(); ?>
    <?php
    // for the first post...
    if($inc == 1) {
    	$my_slider = '[slider]';
    	$my_slider .= load_template_part( 'content', 'homebox' );
    }
    // for the remaining posts...
    else {
    	$my_slider .= '[next-slide]';
    	$my_slider .= load_template_part( 'content', 'homebox' );
    }
    $inc++; // increment the counter
    ?>
    <?php endwhile;
    // end of the loop ?>
    
    <?php wp_reset_postdata(); ?>
    
    <?php
    $my_slider .= '[/slider]';
    
    echo do_shortcode( $my_slider );
    ?>
    $slides = array();
    $slides[] = 'HTML Slide 1';
    $slides[] = 'HTML Slide 2';
    $slides[] = 'HTML Slide 3';
    
    $slider = '[slider]' . implode($slides, '[next-slide]') . '[/slider]';
    Thread Starter hommealone

    (@hommealone)

    Thanks! A nice solution! So, for my application, where I am pulling a template part of a custom post type for every item in the loop, I could adapt your code something like this:

    <?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 $slides = array(); ?>
    <?php // The loop
    while ( $our_query->have_posts() ) : $our_query->the_post(); ?>
    <?php $slides[] = load_template_part( 'content', 'homebox' ); ?>
    
    <?php endwhile;
    // end of the loop ?>
    
    <?php wp_reset_postdata(); ?>
    
    <?php
    $my_slider = '[slider]' . implode($slides, '[next-slide]') . '[/slider]';
    echo do_shortcode( $my_slider );
    ?>
    <?php endif; ?>

    Definitely a nicer solution than mine. Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Slider in theme template using shortcode’ is closed to new replies.