Skip to content

Instantly share code, notes, and snippets.

@helgatheviking
Created April 13, 2012 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save helgatheviking/2379670 to your computer and use it in GitHub Desktop.
Save helgatheviking/2379670 to your computer and use it in GitHub Desktop.
Show attachments as flexslider carousel on single posts
/*
* jQuery FlexSlider v1.8
* http://www.woothemes.com/flexslider/
*
* Copyright 2012 WooThemes
* Free to use under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*/
/* Browser Resets */
.flex-container a:active,
.flexslider a:active,
.flex-container a:focus,
.flexslider a:focus {outline: none;}
.slides,
.flex-control-nav,
.flex-direction-nav {margin: 0; padding: 0; list-style: none;}
/* FlexSlider Necessary Styles
*********************************/
.flexslider {margin: 0; padding: 0;}
.flexslider .slides > li {display: none; -webkit-backface-visibility: hidden;} /* Hide the slides before the JS is loaded. Avoids image jumping */
.flexslider .slides img {max-width: 100%; display: block;}
.flex-pauseplay span {text-transform: capitalize;}
/* Clearfix for the .slides element */
.slides:after {content: "."; display: block; clear: both; visibility: hidden; line-height: 0; height: 0;}
html[xmlns] .slides {display: block;}
* html .slides {height: 1%;}
/* No JavaScript Fallback */
/* If you are not using another script, such as Modernizr, make sure you
* include js that eliminates this class on page load */
.no-js .slides > li:first-child {display: block;}
/* FlexSlider Tweaked Theme
*********************************/
.flexslider {background: #fff; border: 4px solid #fff; position: relative; -webkit-border-radius: 5px; -moz-border-radius: 5px; -o-border-radius: 5px; border-radius: 5px; zoom: 1;}
.flexslider .slides {zoom: 1;}
.flexslider .slides > li {position: relative;}
/* Suggested container for "Slide" animation setups. Can replace this with your own, if you wish */
.flex-container {zoom: 1; position: relative; margin-bottom: 50px;}
/* Caption style */
/* IE rgba() hack */
.flex-caption {background:none; -ms-filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#4C000000,endColorstr=#4C000000);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#4C000000,endColorstr=#4C000000); zoom: 1;}
.flex-caption {width: 96%; padding: 2%; margin: 0; position: absolute; left: 0; bottom: 0; background: rgba(0,0,0,.3); color: #fff; text-shadow: 0 -1px 0 rgba(0,0,0,.3); font-size: 14px; line-height: 18px;}
/* Direction Nav */
.flex-direction-nav { height: 0; }
.flex-direction-nav li a {width: 52px; height: 52px; margin: -13px 0 0; display: block; background: url(images/bg_direction_nav.png) no-repeat; position: absolute; top: 50%; cursor: pointer; text-indent: -999em;}
.flex-direction-nav li .next {background-position: -52px 0; right: -21px;}
.flex-direction-nav li .prev {left: -20px;}
.flex-direction-nav li .disabled {opacity: .3; filter:alpha(opacity=30); cursor: default;}
/* Control Nav */
.flex-control-nav {width: 100%; text-align: center;}
.flex-control-nav li {margin: 0 0 0 5px; display: inline-block; zoom: 1; *display: inline;}
.flex-control-nav li:first-child {margin: 0;}
.flex-control-nav li img { max-width: 50px; max-height: 50px; border: 3px solid transparent; cursor: pointer; }
.flex-control-nav li img:hover {border-color: #eee;}
.flex-control-nav li.active img{border-color: #333; cursor: default;}
<?php
function kia_show_attachments($content){
global $post, $kia_add_script;
if(!is_single()) return $content;
if ( false === ( $attachments_array = get_transient( 'kia_post_attachments' ) ) || !isset($attachments_array[$post->ID] ) ) {
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
$attachments_array[$post->ID] = $attachments;
set_transient( 'kia_post_attachments', $attachments_array );
}
if (!isset($attachments_array[$post->ID])) return $content;
$kia_add_script = true;
$attachments = $attachments_array[$post->ID];
ob_start(); ?>
<div class="flex-container">
<div class="flexslider">
<ul class="slides">
<?php $nav = ''; $counter=0;
foreach ($attachments as $attachment) :
$nav .= '<li><a href="#photo-'.$counter.'">'.wp_get_attachment_image($attachment->ID, 'thumbnail').'<a/></li>';?>
<li><?php echo wp_get_attachment_image($attachment->ID,'large');?></li>
<?php $counter++; endforeach; ?>
</ul>
</div>
<div class="flex-control-nav"><ul><?php echo $nav;?></li></div>
</div>
<?php
$output = ob_get_contents();
ob_end_clean();
return $output . $content;
}
add_filter('the_content','kia_show_attachments');
// refresh transient on save
function kia_delete_attachment_transient($post_id) {
$attachments_array = get_transient( 'kia_post_attachments' );
if(!isset($attachments_array[$post_id])) return $post_id;
//if this post was in the transient, then delete it from the transient
unset($attachments_array[$post_id]);
set_transient( 'kia_post_attachments', $attachments_array );
}
add_action( 'save_post', 'kia_delete_attachment_transient' );
function kia_enqueue_flexslider(){
wp_enqueue_script('jquery');
wp_enqueue_script('flexslider',get_stylesheet_directory_uri() . '/assets/scripts/flexslider/jquery.flexslider-min.js', array('jquery'),true);
wp_enqueue_style('flexslider', get_stylesheet_directory_uri() . '/assets/scripts/flexslider/flexslider.css');
}
function kia_initialize_flexslider(){ ?>
<script type="text/javascript">
jQuery(window).load(function(){
jQuery('.flexslider').flexslider({
controlsContainer:".flex-container",
slideshow: false,
directionNav: false,
manualControls: ".flex-control-nav li"
});
});
</script>
<?php
}
function kia_decide_flexslider(){
global $kia_add_script;
if(!is_single() && false===$kia_add_script) return;
add_action('wp_footer','kia_enqueue_flexslider');
add_action('wp_footer','kia_initialize_flexslider');
}
add_action('wp_enqueue_scripts','kia_decide_flexslider');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment