Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //List all the Tags
- function tags_filter() {
- $tax = 'car_manufacturers';
- $terms = get_terms( $tax );
- $count = count( $terms );
- if ( $count > 0 ): ?>
- <div id="tags2">
- <?php
- foreach ( $terms as $term ) {
- $term_link = get_term_link( $term, $tax );
- echo '<a href="' . $term_link . '" class="tax-filter" type="car" title="' . $term->slug . '">' . $term->name . '</a> ';
- } ?>
- </div>
- <?php endif;
- }
- //Import Scripts
- function ajax_filter_posts_scripts() {
- // Enqueue script
- wp_register_script('afp_script', get_stylesheet_directory_uri() . '/js/ajax-filter-posts.js', false, null, false);
- wp_enqueue_script('afp_script');
- wp_localize_script( 'afp_script', 'afp_vars', array(
- 'afp_nonce' => wp_create_nonce( 'afp_nonce' ), // Create nonce which we later will use to verify AJAX request
- 'afp_ajax_url' => admin_url( 'admin-ajax.php' ),
- )
- );
- }
- add_action('wp_enqueue_scripts', 'ajax_filter_posts_scripts', 100);
- // Script for getting posts
- function ajax_filter_get_posts( $taxonomy, $type ) {
- // Verify nonce
- if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
- die('Permission denied');
- //Custom taxonomy and post type
- $taxonomy = $_POST['taxonomy'];
- $type = $_POST['type'];
- // WP Query
- $args = array(
- 'car_manufacturers' => $taxonomy,
- 'post_type' => $type, // Post type
- 'posts_per_page' => 10,
- );
- // If taxonomy is not set, remove key from array and get all posts
- if( !$taxonomy ) {
- unset( $args['car_manufacturers'] );
- }
- $query = new WP_Query( $args );
- if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
- //This is the grab the featured image thumbnail and display in a grid
- <div class="showroom-cars">
- <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('showroom-thumb'); ?></a>
- <p><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></p>
- </div>
- <?php
- endwhile;
- wp_reset_query(); //resetting the page query
- ?>
- <?php else: ?>
- <h2>No posts found</h2>
- <?php endif;
- die();
- }
- add_action('wp_ajax_filter_posts', 'ajax_filter_get_posts');
- add_action('wp_ajax_nopriv_filter_posts', 'ajax_filter_get_posts');
- //THE JAVASCRIPT
- jQuery(document).ready(function($) {
- $('.tax-filter').click( function() {
- // Prevent default action - opening tag page
- if (event.preventDefault) {
- event.preventDefault();
- } else {
- event.returnValue = false;
- }
- // Get tag slug from title attirbute
- var selecetd_taxonomy = $(this).attr('title');
- var post_type = $(this).attr('type');
- // After user click on tag, fade out list of posts
- $('.tagged-posts').fadeOut();
- data = {
- action: 'filter_posts', // function to execute
- afp_nonce: afp_vars.afp_nonce, // wp_nonce
- taxonomy: selecetd_taxonomy, // selected tag
- type: post_type,
- };
- //I wanted to see what was in 'data'
- console.log(data);
- //I don't understand this part of the code.
- $.post( afp_vars.afp_ajax_url, data, function(response) {
- if( response ) {
- // Display posts on page
- $('.tagged-posts').html( response );
- // Restore div visibility
- $('.tagged-posts').fadeIn();
- };
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement