cptbc_frontend

The CPT Bootstrap Carousel cptbc frontend function.

Description

cptbc_frontend( $atts ); 

Parameters (1)

0. $atts
The atts.

Usage

  1. if ( !function_exists( 'cptbc_frontend' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'cpt-bootstrap-carousel/cptbc-frontend.php'; 
  3.  
  4. // The atts. 
  5. $atts = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = cptbc_frontend($atts); 
  9.  

Defined (1)

The function is defined in the following location(s).

/cptbc-frontend.php  
  1. function cptbc_frontend($atts) { 
  2.  
  3. // Build the attributes 
  4. $id = rand(0, 999); // use a random ID so that the CSS IDs work with multiple on one page 
  5. $args = array( 
  6. 'post_type' => 'cptbc',  
  7. 'posts_per_page' => '-1',  
  8. 'orderby' => $atts['orderby'],  
  9. 'order' => $atts['order'] 
  10. ); 
  11. if($atts['category'] != '') { 
  12. $args['carousel_category'] = $atts['category']; 
  13. if(!isset($atts['before_title'])) $atts['before_title'] = '<h4>'; 
  14. if(!isset($atts['after_title'])) $atts['after_title'] = '</h4>'; 
  15. if(!isset($atts['before_caption'])) $atts['before_caption'] = '<p>'; 
  16. if(!isset($atts['after_caption'])) $atts['after_caption'] = '</p>'; 
  17. if(!isset($atts['image_size'])) $atts['image_size'] = 'full'; 
  18. if(!isset($atts['use_background_images'])) $atts['use_background_images'] = '0'; 
  19. if(!isset($atts['use_javascript_animation'])) $atts['use_javascript_animation'] = '1'; 
  20. if(!isset($atts['select_background_images_style_size'])) $atts['select_background_images_style_size'] = 'cover'; 
  21. if($atts['id'] != '') { 
  22. $args['p'] = $atts['id']; 
  23.  
  24. // Collect the carousel content. Needs printing in two loops later (bullets and content) 
  25. $loop = new WP_Query( $args ); 
  26. $images = array(); 
  27. $output = ''; 
  28. while ( $loop->have_posts() ) { 
  29. $loop->the_post(); 
  30. if ( '' != get_the_post_thumbnail(get_the_ID(), $atts['image_size']) ) { 
  31. $post_id = get_the_ID(); 
  32. $title = get_the_title(); 
  33. $content = get_the_excerpt(); 
  34. $image = get_the_post_thumbnail( get_the_ID(), $atts['image_size'] ); 
  35. $image_src = wp_get_attachment_image_src(get_post_thumbnail_id(), $atts['image_size']); 
  36. $image_src = $image_src[0]; 
  37. $url = get_post_meta(get_the_ID(), 'cptbc_image_url', true); 
  38. $url_openblank = get_post_meta(get_the_ID(), 'cptbc_image_url_openblank', true); 
  39. $link_text = get_post_meta(get_the_ID(), 'cptbc_image_link_text', true); 
  40. $images[] = array('post_id' => $post_id, 'title' => $title, 'content' => $content, 'image' => $image, 'img_src' => $image_src, 'url' => esc_url($url), 'url_openblank' => $url_openblank == "1" ? true : false, 'link_text' => $link_text); 
  41.  
  42. // Check we actually have something to show 
  43. if(count($images) > 0) { 
  44. ob_start(); 
  45. ?> 
  46. <div id="cptbc_<?php echo $id; ?>" class="carousel slide" <?php if($atts['use_javascript_animation'] == '0') { echo ' data-ride="carousel"'; } ?> data-interval="<?php echo $atts['interval']; ?>"> 
  47.  
  48. <?php // First content - the carousel indicators 
  49. if( count( $images ) > 1 ) { ?> 
  50. <ol class="carousel-indicators"> 
  51. <?php foreach ($images as $key => $image) { ?> 
  52. <li data-target="#cptbc_<?php echo $id; ?>" data-slide-to="<?php echo $key; ?>" <?php echo $key == 0 ? 'class="active"' : ''; ?>></li> 
  53. <?php } ?> 
  54. </ol> 
  55. <?php } ?> 
  56.  
  57. <div class="carousel-inner"> 
  58. <?php 
  59. // Carousel Content 
  60. foreach ($images as $key => $image) { 
  61.  
  62. if( !isset($atts['link_button']) ) { 
  63. $atts['link_button'] = 0; 
  64.  
  65. // Build anchor link so it can be reused 
  66. $linkstart = ''; 
  67. $linkend = ''; 
  68. if($image['url'] && $atts['link_button'] == 0) { 
  69. $linkstart = '<a href="'.$image['url'].'"'; 
  70. if($image['url_openblank']) { 
  71. $linkstart .= ' target="_blank"'; 
  72. $linkstart .= '>'; 
  73. $linkend = '</a>'; 
  74. } ?> 
  75.  
  76. <div class="item <?php echo $key == 0 ? 'active' : ''; ?>" id="cptbc-item-<?php echo $image['post_id']; ?>" <?php if($atts['use_background_images'] == 1) { echo ' style="height: '.$atts['background_images_height'].'px; background: url(\''.$image['img_src'].'\') no-repeat center center ; -webkit-background-size: ' . $atts['select_background_images_style_size'] . '; -moz-background-size: ' . $atts['select_background_images_style_size'] . '; -o-background-size: ' . $atts['select_background_images_style_size'] . '; background-size: ' . $atts['select_background_images_style_size'] . ';"'; } ?>> 
  77. <?php 
  78. // Regular behaviour - display image with link around it 
  79. if($atts['use_background_images'] == 0) { 
  80. echo $linkstart.$image['image'].$linkend; 
  81. // Backgorund images mode - need block level link inside carousel link if we have a linl 
  82. } else if($image['url'] && $atts['link_button'] == 0) { 
  83. echo '<a href="'.$image['url'].'"'; 
  84. if($image['url_openblank']) { 
  85. $linkstart .= ' target="_blank"'; 
  86. echo ' style="display:block; width:100%; height:100%;"> </a>'; 
  87. }  
  88. // The Caption div 
  89. if(($atts['showcaption'] === 'true' && (strlen($image['title']) > 0 || strlen($image['content']) > 0)) || ($image['url'] && $atts['link_button'] == 1)) { 
  90. echo '<div class="carousel-caption">'; 
  91. // Title 
  92. if(strlen($image['title']) > 0) { 
  93. echo $atts['before_title'].$linkstart.$image['title'].$linkend.$atts['after_title']; 
  94. // Caption 
  95. if(strlen($image['content']) > 0) { 
  96. echo $atts['before_caption'].$linkstart.$image['content'].$linkend.$atts['after_caption']; 
  97. // Link Button 
  98. if($image['url'] && $atts['link_button'] == 1) {  
  99. if(isset($atts['link_button_before'])) echo $atts['link_button_before']; 
  100. $target = ''; 
  101. if($image['url_openblank']) { 
  102. $target = ' target="_blank"'; 
  103. echo '<a href="'.$image['url'].'" '.$target.' class="'.$atts['link_button_class'].'">'; 
  104. if(isset($image['link_text']) && strlen($image['link_text']) > 0) { 
  105. echo $image['link_text']; 
  106. } else { 
  107. echo $atts['link_button_text']; 
  108. echo '</a>'; 
  109. if(isset($atts['link_button_after'])) echo $atts['link_button_after']; 
  110. echo '</div>'; 
  111. } ?> 
  112. </div> 
  113. <?php } ?> 
  114. </div> 
  115.  
  116. <?php // Previous / Next controls 
  117. if( count( $images ) > 1 ) { 
  118. if($atts['showcontrols'] === 'true' && $atts['twbs'] == '3') { ?> 
  119. <a class="left carousel-control" href="#cptbc_<?php echo $id; ?>" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a> 
  120. <a class="right carousel-control" href="#cptbc_<?php echo $id; ?>" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a> 
  121. <?php } else if($atts['showcontrols'] === 'true') { ?> 
  122. <a class="left carousel-control" href="#cptbc_<?php echo $id; ?>" data-slide="prev">*</a> 
  123. <a class="right carousel-control" href="#cptbc_<?php echo $id; ?>" data-slide="next">*</a> 
  124. <?php } else if($atts['showcontrols'] === 'custom' && $atts['twbs'] == '3' && $atts['customprev'] != '' && $atts['customnext'] != '') { ?> 
  125. <a class="left carousel-control" href="#cptbc_<?php echo $id; ?>" data-slide="prev"><span class="<?php echo $atts['customprev'] ?> icon-prev"></span></a> 
  126. <a class="right carousel-control" href="#cptbc_<?php echo $id; ?>" data-slide="next"><span class="<?php echo $atts['customnext'] ?> icon-next"></span></a> 
  127. <?php } 
  128. } ?> 
  129.  
  130. </div> 
  131.  
  132. <?php // Javascript animation fallback 
  133. if($atts['use_javascript_animation'] == '1') { ?> 
  134. <script type="text/javascript"> 
  135. jQuery(document).ready(function() { 
  136. jQuery('#cptbc_<?php echo $id; ?>').carousel({ 
  137. interval: <?php echo $atts['interval']; ?> 
  138. }); 
  139. }); 
  140. </script> 
  141. <?php } 
  142.  
  143. // Collect the output 
  144. $output = ob_get_contents(); 
  145. ob_end_clean(); 
  146.  
  147. // Restore original Post Data 
  148. wp_reset_postdata();  
  149.  
  150. return $output;