Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Slider loads Custom Post Type Slides twice WordPress

  • SOLVED

Found some code to get the Nivo Slider to load a title and excertpt + featured image from a custom post type in the backend - see [[LINK href="http://markwilkinson.me/2011/06/using-nivo-slider-with-wordpress-custom-post-types/"]]http://markwilkinson.me/2011/06/using-nivo-slider-with-wordpress-custom-post-types/[[/LINK]] . Somehow the slides are loaded twice all the time. Any ideas why?

Custom post code in functions.php:


*----------------------------------------
Slider Custom Post Type
---------------------------------------------*/

add_action( 'init', 'create_slider' );
function create_slider() {
register_post_type( 'img_slideshow',
array(
'labels' => array(
'name' => __( 'Slides' ),
'singular_name' => __( 'Slide' )
),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
)
);
}


Code loading the Custom Post Type slides:

<div id="slider" class="slider-wrapper theme-default">
<?php $pj_slider = new WP_Query('post_type=img_slideshow&showposts=4'); while($pj_slider->have_posts()) : $pj_slider->the_post(); ?>

<?php $pj_slider_caption = '#slider-caption-'.get_the_ID(); ?>

<?php if(has_post_thumbnail() ) { ?>
<?php the_post_thumbnail('slider', array('title' => ''.$pj_slider_caption.'')); ?>
<?php } ?>

<?php endwhile; ?>

<?php $pj_slider_caption = new WP_Query('post_type=img_slideshow&showposts=4');
while($pj_slider_caption->have_posts()) : $pj_slider_caption->the_post(); ?>

<div id="slider-caption- <?php the_ID(); ?>" class="nivo-html-caption">

<span class="nivo-caption-title"><?php the_title(); ?></span>
<?php $pj_nivo_caption_content = get_the_excerpt(); ?>
<span class="nivo-caption-content"><?php echo $pj_nivo_caption_content; ?></span>
<span class="nivo-caption-link"><a href="<?php the_permalink(); ?>">Read more about this feature &raquo;</a></span>

</div><!-- // nivo-html-caption -->

<?php endwhile; ?>

</div> <!-- end slider -->

Answers (3)

2012-05-21

Arnav Joy answers:

try this

<div id="slider" class="slider-wrapper theme-default">

<?php $pj_slider = new WP_Query('post_type=img_slideshow&showposts=4'); while($pj_slider->have_posts()) : $pj_slider->the_post(); ?>



<?php $pj_slider_caption = '#slider-caption-'.get_the_ID(); ?>



<?php if(has_post_thumbnail() ) { ?>

<?php the_post_thumbnail('slider', array('title' => ''.$pj_slider_caption.'')); ?>

<?php } ?>



<?php endwhile; wp_reset_query(); ?>



<?php $pj_slider_caption = new WP_Query('post_type=img_slideshow&showposts=4');

while($pj_slider_caption->have_posts()) : $pj_slider_caption->the_post(); ?>



<div id="slider-caption- <?php the_ID(); ?>" class="nivo-html-caption">



<span class="nivo-caption-title"><?php the_title(); ?></span>

<?php $pj_nivo_caption_content = get_the_excerpt(); ?>

<span class="nivo-caption-content"><?php echo $pj_nivo_caption_content; ?></span>

<span class="nivo-caption-link"><a href="<?php the_permalink(); ?>">Read more about this feature &raquo;</a></span>



</div><!-- // nivo-html-caption -->



<?php endwhile; ?>



</div> <!-- end slider -->


Arnav Joy comments:

try this

<div id="slider" class="slider-wrapper theme-default">

<?php $pj_slider = new WP_Query('post_type=img_slideshow&showposts=4'); while($pj_slider->have_posts()) : $pj_slider->the_post(); ?>



<?php $pj_slider_caption = '#slider-caption-'.get_the_ID(); ?>



<?php if(has_post_thumbnail() ) {

$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'slider');
?>
<img src="<?php echo $large_image_url[0];?>" title="<?php echo $pj_slider_caption;?>" alt="<?php echo $pj_slider_caption;?>" />


<?php } ?>



<?php endwhile; wp_reset_query(); ?>



<?php $pj_slider_caption = new WP_Query('post_type=img_slideshow&showposts=4');

while($pj_slider_caption->have_posts()) : $pj_slider_caption->the_post(); ?>



<div id="slider-caption- <?php the_ID(); ?>" class="nivo-html-caption">



<span class="nivo-caption-title"><?php the_title(); ?></span>

<?php $pj_nivo_caption_content = get_the_excerpt(); ?>

<span class="nivo-caption-content"><?php echo $pj_nivo_caption_content; ?></span>

<span class="nivo-caption-link"><a href="<?php the_permalink(); ?>">Read more about this feature &raquo;</a></span>



</div><!-- // nivo-html-caption -->



<?php endwhile; ?>



</div> <!-- end slider -->


rhand comments:

I still have two slides per custom post type using this code.

2012-05-21

Jurre Hanema answers:

You do realize that you are creating and executing two different post loops, each of which displays something?

So displaying the posts twice is exactly what this code is supposed to do.

I am not sure what you want the code to do, but I suggest removing this (the first loop) part:


<?php $pj_slider = new WP_Query('post_type=img_slideshow&showposts=4'); while($pj_slider->have_posts()) : $pj_slider->the_post(); ?>



<?php $pj_slider_caption = '#slider-caption-'.get_the_ID(); ?>



<?php if(has_post_thumbnail() ) { ?>

<?php the_post_thumbnail('slider', array('title' => ''.$pj_slider_caption.'')); ?>

<?php } ?>



<?php endwhile; ?>


rhand comments:

Removing the first loop makes the Nivo Slider load three empty slides while the posts do exist and the featured images are there.

2012-05-21

Nilesh shiragave answers:

try this


<div id="slider" class="slider-wrapper theme-default">

<?php $pj_slider = new WP_Query('post_type=img_slideshow&showposts=4'); while($pj_slider->have_posts()) : $pj_slider->the_post(); ?>



<?php $pj_slider_caption = '#slider-caption-'.get_the_ID(); ?>



<?php if(has_post_thumbnail() ) { ?>

<?php the_post_thumbnail('slider', array('title' => ''.$pj_slider_caption.'')); ?>

<?php } ?>

<div id="slider-caption- <?php the_ID(); ?>" class="nivo-html-caption">



<span class="nivo-caption-title"><?php the_title(); ?></span>

<?php $pj_nivo_caption_content = get_the_excerpt(); ?>

<span class="nivo-caption-content"><?php echo $pj_nivo_caption_content; ?></span>

<span class="nivo-caption-link"><a href="<?php the_permalink(); ?>">Read more about this feature &raquo;</a></span>



</div><!-- // nivo-html-caption -->

<?php endwhile; ?>





</div> <!-- end slider -->


Nilesh shiragave comments:

Let me know how html code you want for nivo slider without any PHP code..


Nilesh shiragave comments:

try this code.....


<div class="slider-wrapper theme-default">
<div class="ribbon"></div>
<div id="slider" class="nivoSlider">

<?php $pj_slider = new WP_Query('post_type=img_slideshow&showposts=4'); while($pj_slider->have_posts()) : $pj_slider->the_post(); ?>



<?php $pj_slider_caption = '#slider-caption-'.get_the_ID(); ?>



<?php if(has_post_thumbnail() ) { ?>

<?php the_post_thumbnail('slider', array('title' => ''.$pj_slider_caption.'')); ?>

<?php } ?>



<?php endwhile; wp_reset_query(); ?>

</div>
<?php $pj_slider_caption = new WP_Query('post_type=img_slideshow&showposts=4');

while($pj_slider_caption->have_posts()) : $pj_slider_caption->the_post(); ?>



<div id="slider-caption- <?php the_ID(); ?>" class="nivo-html-caption">



<span class="nivo-caption-title"><?php the_title(); ?></span>

<?php $pj_nivo_caption_content = get_the_excerpt(); ?>

<span class="nivo-caption-content"><?php echo $pj_nivo_caption_content; ?></span>

<span class="nivo-caption-link"><a href="<?php the_permalink(); ?>">Read more about this feature &raquo;</a></span>



</div><!-- // nivo-html-caption -->



<?php endwhile; wp_reset_query(); ?>
</div>


rhand comments:

Your last code does make only three slides load, but the captions do not load now. I do see a tiny caption background bar, but no text.


rhand comments:

Your code, plus one small fix

<div id="slider-caption-<?php the_ID(); ?>" class="nivo-html-caption">
without the space after slider-caption
brought the captions back. Thanks


Nilesh shiragave comments:

just caption text is not coming right? Read more about this feature link is working?


Nilesh shiragave comments:

GREAT :)