s-hype

Untitled

Apr 24th, 2014
163
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //List all the Tags
  2. function tags_filter() {
  3. $tax = 'car_manufacturers';
  4. $terms = get_terms( $tax );
  5. $count = count( $terms );
  6.  
  7. if ( $count > 0 ): ?>
  8. <div id="tags2">
  9. <?php
  10. foreach ( $terms as $term ) {
  11. $term_link = get_term_link( $term, $tax );
  12. echo '<a href="' . $term_link . '" class="tax-filter" type="car" title="' . $term->slug . '">' . $term->name . '</a> ';
  13. } ?>
  14. </div>
  15. <?php endif;
  16. }
  17.  
  18. //Import Scripts
  19. function ajax_filter_posts_scripts() {
  20. // Enqueue script
  21. wp_register_script('afp_script', get_stylesheet_directory_uri() . '/js/ajax-filter-posts.js', false, null, false);
  22. wp_enqueue_script('afp_script');
  23.  
  24. wp_localize_script( 'afp_script', 'afp_vars', array(
  25. 'afp_nonce' => wp_create_nonce( 'afp_nonce' ), // Create nonce which we later will use to verify AJAX request
  26. 'afp_ajax_url' => admin_url( 'admin-ajax.php' ),
  27. )
  28. );
  29. }
  30. add_action('wp_enqueue_scripts', 'ajax_filter_posts_scripts', 100);
  31.  
  32.  
  33. // Script for getting posts
  34. function ajax_filter_get_posts( $taxonomy, $type ) {
  35.  
  36. // Verify nonce
  37. if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
  38. die('Permission denied');
  39.  
  40. //Custom taxonomy and post type
  41. $taxonomy = $_POST['taxonomy'];
  42. $type = $_POST['type'];
  43.  
  44. // WP Query
  45. $args = array(
  46. 'car_manufacturers' => $taxonomy,
  47. 'post_type' => $type, // Post type
  48. 'posts_per_page' => 10,
  49. );
  50.  
  51. // If taxonomy is not set, remove key from array and get all posts
  52. if( !$taxonomy ) {
  53. unset( $args['car_manufacturers'] );
  54. }
  55.  
  56. $query = new WP_Query( $args );
  57.  
  58. if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
  59.  
  60. //This is the grab the featured image thumbnail and display in a grid
  61. <div class="showroom-cars">
  62. <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('showroom-thumb'); ?></a>
  63. <p><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></p>
  64. </div>
  65.  
  66. <?php
  67. endwhile;
  68. wp_reset_query(); //resetting the page query
  69. ?>
  70. <?php else: ?>
  71. <h2>No posts found</h2>
  72. <?php endif;
  73.  
  74. die();
  75. }
  76.  
  77. add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
  78. add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');
  79.  
  80.  
  81.  
  82. //THE JAVASCRIPT
  83. jQuery(document).ready(function($) {
  84. $('.tax-filter').click( function() {
  85.  
  86. // Prevent default action - opening tag page
  87. if (event.preventDefault) {
  88. event.preventDefault();
  89. } else {
  90. event.returnValue = false;
  91. }
  92.  
  93. // Get tag slug from title attirbute
  94. var selecetd_taxonomy = $(this).attr('title');
  95. var post_type = $(this).attr('type');
  96.  
  97. // After user click on tag, fade out list of posts
  98. $('.tagged-posts').fadeOut();
  99.  
  100. data = {
  101. action: 'filter_posts', // function to execute
  102. afp_nonce: afp_vars.afp_nonce, // wp_nonce
  103. taxonomy: selecetd_taxonomy, // selected tag
  104. type: post_type,
  105. };
  106.  
  107. //I wanted to see what was in 'data'
  108. console.log(data);
  109.  
  110. //I don't understand this part of the code.
  111. $.post( afp_vars.afp_ajax_url, data, function(response) {
  112.  
  113. if( response ) {
  114. // Display posts on page
  115. $('.tagged-posts').html( response );
  116. // Restore div visibility
  117. $('.tagged-posts').fadeIn();
  118. };
  119. });
  120. });
  121. });
RAW Paste Data