Skip to content

Instantly share code, notes, and snippets.

@designbuildtest
Last active August 29, 2015 14:04
Show Gist options
  • Save designbuildtest/562bcd4ee45e14be3d9e to your computer and use it in GitHub Desktop.
Save designbuildtest/562bcd4ee45e14be3d9e to your computer and use it in GitHub Desktop.
Auto-advance slider
/**
* Add an option to auto-advance the Featured Content Slider.
*/
$wp_customize->add_setting( 'featured_content_autoplay', array(
'default' => 'off',
'sanitize_callback' => 'mytheme_sanitize_autoplay',
) );
$wp_customize->add_control( 'featured_content_autoplay', array(
'label' => __( 'Autoplay', 'sendtheme' ),
'section' => 'featured_content',
'type' => 'select',
'choices' => array(
'off' => __( 'Off', 'sendtheme' ),
'on' => __( 'On', 'sendtheme' ),
),
'priority' => 40,
) );
/**
* Sanitize the Featured Content Slider autoplay value.
*/
function mytheme_sanitize_autoplay( $autoplay ) {
if ( ! in_array( $autoplay, array( 'off', 'on' ) ) ) {
$autoplay = 'off';
}
return $autoplay;
}
/**
* If auto-advance has been enabled for the Featured Content slider, add the appropriate code.
*/
if ( get_theme_mod( 'featured_content_autoplay' ) === 'on' ) :
/**
* Add some custom jQuery code to enable the Featured Content slider to auto advance.
* http://wordpress.org/support/topic/twenty-fourteen-slider-not-slide?replies=4#post-5046422
*/
function mytheme_add_slider_autoplay_script() { ?>
<script type="text/javascript" charset="utf-8">
jQuery(document).ready(function(){
var change_every = 8;
var current = 1;
function auto_advance(){
if(current == -1) return false;
jQuery('.flexslider .flex-next').eq(current % jQuery('.flexslider .flex-next').length).trigger('click', [true]);
current++;
};
setInterval(function(){auto_advance()}, change_every * 1000);
});
</script>
<?php }
add_action('wp_footer', 'mytheme_add_slider_autoplay_script');
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment