Website:
Paste Title:
Colors
Sizes
1
2
3
4
5
6
7
// I've changed the function to display tags based on the CPT // Like tags_filter('books'); function tags_filter($rel) { $tax = 'post_tag'; //get a list of all post of your type $args = array( 'posts_per_page' => -1, 'post_type' => $rel ); $terms= array(); $posts = get_posts($args); foreach($posts as $p){ //get all terms of your taxonomy for each type $ts = wp_get_object_terms($p->ID,$tax); foreach ( $ts as $t ) { if (!in_array($t,$terms)){ //only add this term if its not there yet $terms[] = $t; } } } //when you get here $terms is an array of term objects that have posts of your custom type //so just print them out. echo '<div class="tag-list">'; foreach($terms as $tr){ // Apply the post type based on the link attribute echo '<a href="' . get_term_link($tr->slug, $tax) . '" class="tax-filter" tipo="' . $rel . '" title="' . $tr->slug . '">'.$tr->name.'</a>'; } echo '</div>'; wp_reset_postdata(); } // 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'); $taxonomy = $_POST['taxonomy']; $type = $_POST['type']; // WP Query $args = array( 'tag' => $taxonomy, 'post_type' => $type, 'posts_per_page' => 10, ); // If taxonomy is not set, remove key from array and get all posts if( !$taxonomy ) { unset( $args['tag'] ); } $query = new WP_Query( $args ); if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php the_excerpt(); ?> <?php endwhile; ?> <?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'); // Here's the javascript function jQuery(document).ready(function($) { $('.tax-filter').click( function() { // Prevent defualt action - opening tag page if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; } // Get tag slug from title attirbute var selected_taxonomy = $(this).attr('title'); var post_type = $(this).attr('tipo'); $('.tagged-posts').fadeOut(); data = { action: 'filter_posts', afp_nonce: afp_vars.afp_nonce, taxonomy: selected_taxonomy, type: post_type, }; $.post( afp_vars.afp_ajax_url, data, function(response) { if( response ) { $('.tagged-posts').html( response ); $('.tagged-posts').fadeIn(); }; }); }); });
Password to view?
Enable code highlighting?
No
Yes