Filtro categorie ajax
So che è una richiesta importante, ma non riesco a capire dove sia il problema.
Ho trovato questa pagina: http://www.bobz.co/ajax-filter-posts-tag/#comment-28112
Mostra come creare un filtro dinamico per i tag dei post.
Volevo modificarlo per le categorie dei post, ma non riesco a farlo funzionare.
Ho inserito questo codice nel mio functions.php
function ajax_filter_posts_scripts() {
// Registra lo 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' ), // Crea nonce che useremo dopo per verificare la richiesta AJAX
'afp_ajax_url' => admin_url( 'admin-ajax.php' ),
)
);
}
add_action('wp_enqueue_scripts', 'ajax_filter_posts_scripts', 100);
// Script per ottenere i post
function ajax_filter_get_posts( $taxonomy ) {
// Verifica nonce
if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
die('Permesso negato');
$taxonomy = $_POST['taxonomy'];
// WP Query
$args = array(
'category_name' => $taxonomy,
'post_type' => 'post',
'posts_per_page' => 10,
);
// Se la tassonomia non è impostata, rimuovi la chiave dall'array e ottieni tutti i post
if( !$taxonomy ) {
unset( $args['category_name'] );
}
$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>Nessun post trovato</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');
e poi nel mio template di pagina ho inserito questo codice:
//nel mio file template
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
);
$query = new WP_Query( $args );
function tags_filter() {
$tax = 'category';
$terms = get_terms( $tax );
$count = count( $terms );
if ( $count > 0 ): ?>
<div class="post-tags">
<?php
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $tax );
echo '<a href="' . $term_link . '" class="tax-filter" title="' . $term->slug . '">' . $term->name . '</a> ';
} ?>
</div>
<?php endif;
}
Quando carico il mio template di pagina, il sito carica i contenuti e mostra i pulsanti del filtro delle categorie, ma quando clicco su uno qualsiasi dei pulsanti, restituisce "Nessun post trovato".
Questo mi porta a pensare che ho fatto qualcosa di sbagliato nel mio file functions, ma non riesco a capire cosa.
Qualcuno riesce a vedere dove ho sbagliato?
Non sono sicuro che tu abbia già risolto questo problema, ma stavo cercando un modo per incorporarlo in una pagina e filtrare i post per categoria.
Sono riuscito a farlo funzionare in modo che mostri tutte le categorie e i post correlati. Inserisci questo codice nel functions.php
function ajax_filter_posts_scripts() {
// Includi lo script
wp_register_script('afp_script', get_template_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' ), // Crea un nonce che useremo dopo per verificare la richiesta AJAX
'afp_ajax_url' => admin_url( 'admin-ajax.php' ),
)
);
}
add_action('wp_enqueue_scripts', 'ajax_filter_posts_scripts', 100);
// Script per ottenere i post
function ajax_filter_get_posts( $taxonomy ) {
// Verifica il nonce
if( !isset( $_POST['afp_nonce'] ) || !wp_verify_nonce( $_POST['afp_nonce'], 'afp_nonce' ) )
die('Permesso negato');
$taxonomy = $_POST['taxonomy'];
// WP Query
$args = array(
'category_name' => $taxonomy,
'post_type' => 'post',
'posts_per_page' => 10,
);
echo $taxonomy;
// Se la tassonomia non è impostata, rimuovi la chiave dall'array e ottieni tutti i post
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>Nessun post trovato</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');
Poi, aggiungi questo nel tuo template di pagina:
<?php $args = array(
'post_type' => 'post',
'posts_per_page' => 10,
);
$query = new WP_Query( $args );
$tax = 'category';
$terms = get_terms( $tax );
$count = count( $terms );
if ( $count > 0 ): ?>
<div class="post-tags">
<?php
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $tax );
echo '<a href="' . $term_link . '" class="tax-filter" title="' . $term->slug . '">' . $term->name . '</a> ';
} ?>
</div>
<?php endif;
if ( $query->have_posts() ): ?>
<div class="tagged-posts">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php endwhile; ?>
</div>
<?php else: ?>
<div class="tagged-posts">
<h2>Nessun post trovato</h2>
</div>
<?php endif; ?>
Spero che questo ti aiuti a risolvere i tuoi problemi!
