Skip to content

Instantly share code, notes, and snippets.

@setola
Created May 21, 2012 23:19
Show Gist options
  • Save setola/2765340 to your computer and use it in GitHub Desktop.
Save setola/2765340 to your computer and use it in GitHub Desktop.
A simple way to manage a slideshow with WordPress
<?php
add_theme_support('post-thumbnails');
add_image_size('slideshow-mini', 870, 500, true);
<?php
$images = get_attachments_by_media_tags('media_tags=slideshow');
echo slideshow($images);
<div id="carousel-4fbea6cb57c5b" class="carousel slide">
<div class="carousel-inner">
<div class="item active">
<img width="870" height="500"
src="http://www.emanueletessore.com/wp-content/uploads/2012/05/bootstrap-mdo-sfmoma-03.jpg"
class="attachment-slideshow-mini"
alt="Third Thumbnail alternate text" title="Third Thumbnail label" />
<div class="carousel-caption">
<h4>Third Thumbnail label</h4>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam.
Donec id elit non mi porta gravida at eget metus. Nullam id dolor
id nibh ultricies vehicula ut id elit.</p>
</div>
</div>
<div class="item">
<!-- second item here -->
</div>
<div class="item">
<!-- third item here -->
</div>
</div>
<a class="carousel-control left" href="#carousel-4fbea6cb57c5b"
data-slide="prev">&lsaquo;</a> <a class="carousel-control right"
href="#carousel-4fbea6cb57c5b" data-slide="next">&rsaquo;</a>
</div>
<?php
function slideshow($images=null){
if(isset($images)){
$images = (array)get_children( 'post_type=attachment&post_mime_type=image&post_parent='.get_the_ID() );
}
$uid = uniqid('carousel-');
$tpl = <<< EOF
<div class="item%active%">
%img%
<div class="carousel-caption">
<h4>%title%</h4>
%description%
</div>
</div>
EOF;
$prepend = '<div id="'.$uid.'" class="carousel slide">
<div class="carousel-inner">';
$append = '</div>
<a class="carousel-control left" href="#'.$uid.'" data-slide="prev">&lsaquo;</a>
<a class="carousel-control right" href="#'.$uid.'" data-slide="next">&rsaquo;</a>
</div>';
$toret = '';
$size = 'slideshow-mini';
$is_first = true;
foreach($images as $img) {
$alt = get_post_meta($img->ID, '_wp_attachment_image_alt', true);
$toret .= str_replace(
array('%img%','%title%','%description%', '%active%'),
array(
wp_get_attachment_image(
$img->ID,
$size,
false,
array(
'class' => "attachment-$size",
'alt' => empty($alt) ? ' ' : trim(esc_attr(strip_tags($alt))),
'title' => empty($img->post_title) ? '' : trim(esc_attr(strip_tags($img->post_title))),
)
),
get_the_title($img->ID),
apply_filters('the_content', $img->post_content),
($is_first) ? ' active' : ''
),
$tpl
);
$is_first = false;
}
return $prepend.$toret.$append;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment