Cycle2 Plugin hide controls

Cycle2 Plugin hide controls

Hi there,

I am using the cycle2 plugin integrated into the wordpress gallery and works like a charm. What i would like it to do is when there is only 1 image to hide the controls and when more than one show them. I spoke to Mike on Github and he gave me a snippet of code and with my additional lines it doesn't seem to work still. If anyone can spot how to resolve this i'd be grateful.



Function to hide controls (this is in my header under the script links to jQuery and plugins)

  1. $('.cycle-slideshow').on( 'cycle-initialized', function( e, opts ) {
  2. if ( opts.slideCount < 1 ) {
  3. $(".center").fadeToggle("display", "none");
  4. }
  5. });

CSS:

  1. .cycle-slideshow {
  2. height:400px;
  3. z-index:0;
  4. }
  5. .cycle-slideshow  img{
  6. width:100%;
  7. height:100%;
  8. z-index:3;
  9. }
  10. .center {
  11. display:block;
  12. }
  13. .center a{
  14. z-index:4;
  15. position:absolute;
  16. margin-top:-48px;
  17. }
  18. .center a:hover {
  19. display:block;
  20. }
  21. .center a#prev, .center a#next{
  22. position:relative;
  23. width:4%;
  24. height:60px;
  25. width:60px;
  26. margin-top:-60px;
  27. font-size:40px;
  28. text-align:center;
  29. color:#FFF;
  30. }
  31. .center a#next{
  32. float:right;
  33. background:url(images/next.png) center -2px no-repeat;
  34. }
  35. .center a#prev {
  36. float:left;
  37. background:url(images/prev.png) center -2px no-repeat;
  38. }



Integrated Slideshow in Wordpress Gallery (functions.php)

  1. function custom_gallery( $output, $attr ){
  2.     global $post, $wp_locale;

  3.     static $instance = 0;
  4. $instance++;

  5. // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  6. if ( isset( $attr['orderby'] ) ) {
  7.     $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  8.     if ( !$attr['orderby'] )
  9.         unset( $attr['orderby'] );
  10. }

  11. extract(shortcode_atts(array(
  12.     'order'      => 'ASC',
  13.     'orderby'    => 'menu_order ID',
  14.     'id'         => $post->ID,
  15.     'itemtag'    => 'div',
  16.     'columns'    => 1,
  17.     'size'       => 'full',
  18.     'include'    => '',
  19.     'exclude'    => ''
  20. ), $attr));

  21. $id = intval($id);
  22. if ( 'RAND' == $order )
  23.     $orderby = 'none';

  24. if ( !empty($include) ) {
  25.     $include = preg_replace( '/[^0-9,]+/', '', $include );
  26.     $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );

  27.     $attachments = array();
  28.     foreach ( $_attachments as $key => $val ) {
  29.         $attachments[$val->ID] = $_attachments[$key];
  30.     }
  31. } elseif ( !empty($exclude) ) {
  32.     $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
  33.     $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  34. } else {
  35.     $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  36. }

  37. if ( empty($attachments) )
  38.     return '';

  39. if ( is_feed() ) {
  40.     $output = "\n";
  41.     foreach ( $attachments as $att_id => $attachment )
  42.         $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
  43.     return $output;
  44. }

  45. $itemtag = tag_escape($itemtag);
  46. $captiontag = tag_escape($captiontag);
  47. $columns = intval($columns);
  48. $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  49. $float = is_rtl() ? 'right' : 'left';

  50. $selector = "gallery-{$instance}";


  51. $size_class = sanitize_html_class( $size );
  52. $gallery_div = "<div class=\"cycle-slideshow\" data-cycle-fx=\"scrollHorz\" data-cycle-pause-on-hover=\"true\" data-cycle-speed=\"200\"  data-cycle-next=\"#next\"  data-cycle-prev=\"#prev\" data-cycle-swipe=\"true\">";
  53. $output = apply_filters( 'gallery_style', $gallery_div );

  54. $i = 0;
  55. foreach ( $attachments as $id => $attachment ) {
  56.     $image = wp_get_attachment_image( $id, $size );
  57.     $output .= "";
  58.     $output .= $image;
  59.     $output .= "";
  60. }

  61. $output .= "
  62.     </div>
  63. <div class='center'>
  64.         <a href='javascript:void(0);' id='prev'>&nbsp;</a> 
  65.         <a href='javascript:void(0);' id='next'>&nbsp;</a>
  66. </div>
  67. \n";

  68. return $output;
  69. }
  70. add_filter('post_gallery', 'custom_gallery', 11, 2);