tzvij

part 3 of FAQ code

Nov 13th, 2013
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.48 KB | None | 0 0
  1. /*
  2.  * Add [rc_faq limit="-1"] shortcode
  3.  *
  4.  */
  5. function rc_faq_shortcode( $atts, $content = null ) {
  6.    
  7.     extract(shortcode_atts(array(
  8.         "limit" => ''
  9.     ), $atts));
  10.    
  11.     // Define limit
  12.     if( $limit ) {
  13.         $posts_per_page = $limit;
  14.     } else {
  15.         $posts_per_page = '-1';
  16.     }
  17.    
  18.     ob_start();
  19.  
  20.     // Create the Query
  21.     $post_type      = 'rc_faq';
  22.     $orderby        = 'menu_order';
  23.     $order          = 'ASC';
  24.                
  25.     $query = new WP_Query( array (
  26.                                 'post_type'      => $post_type,
  27.                                 'posts_per_page' => $posts_per_page,
  28.                                 'orderby'        => $orderby,
  29.                                 'order'          => $order,
  30.                                 'no_found_rows'  => 1
  31.                                 )
  32.                         );
  33.    
  34.     //Get post type count
  35.     $post_count = $query->post_count;
  36.     $i = 1;
  37.    
  38.     // Displays FAQ info
  39.     if( $post_count > 0) :
  40.    
  41.         // Loop
  42.         while ($query->have_posts()) : $query->the_post();
  43.         ?>
  44.        
  45.         <h3 class="rc_faq_title"><a href="#" onclick="rc_faq_toggle('rc_faq_<?php echo get_the_ID(); ?>');"><?php the_title(); ?></a></h3>
  46.         <p id="rc_faq_<?php echo get_the_ID(); ?>" style="display: none;"><?php echo get_the_content(); ?></p>
  47.  
  48.         <?php
  49.         $i++;
  50.         endwhile;
  51.        
  52.     endif;
  53.    
  54.     // Reset query to prevent conflicts
  55.     wp_reset_query();
  56.    
  57.     ?>
  58.     <script type="text/javascript">
  59.     <!--
  60.         function rc_faq_toggle(id) {
  61.             var e = document.getElementById(id);
  62.             e.style.display = ((e.style.display!='none') ? 'none' : 'block');
  63.         }
  64.     //-->
  65.     </script>
  66.     <?php
  67.    
  68.     return ob_get_clean();
  69.  
  70. }
  71.  
  72. add_shortcode("rc_faq", "rc_faq_shortcode");
Advertisement
Add Comment
Please, Sign In to add comment