WP_Query non mostra post type personalizzato, loop multipli e get_template_part
Sto avendo problemi a far visualizzare i miei testimonial. Su page.php ho una sezione dedicata ai testimonial. Quindi ho un loop che recupera il contenuto per page.php, e poi un get_template_part('content', 'testimonials') per ottenere il loop dei testimonial.
<div class="grid_12 omega clearfix">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<hr>
<?php endwhile; else : ?>
<p>Non ci sono articoli :( </p>
<?php endif; ?>
</div>
<?php get_template_part( 'content', 'testimonials' ); ?>
Il mio loop principale è quello sopra ^^. Il mio loop per i testimonial si trova in content-testimonials.php e appare così:
<?php
$args = array(
'post_type' => 'testimonials',
'posts_per_page' => 1,
'orderby' => 'rand'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="testimonial push_2 grid_10 clearfix">
<blockquote>“<?php the_field( 'testimonial' ); ?>”</blockquote>
<cite>—<?php the_field( 'name' ); ?></cite>
</div>
<?php endwhile; else : ?>
<p>Non ci sono testimonial :( </p>
<?php endif; ?>
Sono ancora abbastanza nuovo in WordPress, quindi non riesco a capire cosa mi manca. Ho controllato nell'area di amministrazione per verificare che ci fossero testimonial, ho modificato il custom field per visualizzare se il post type è uguale a testimonial, ma non capisco perché non venga caricato.
Attendo con interesse le vostre risposte, grazie in anticipo!
Penso che tu abbia dimenticato di verificare se $the_query ha post.
<?php
$args = array(
'post_type' => 'testimonials',
'posts_per_page' => 1,
'orderby' => 'rand'
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="testimonial push_2 grid_10 clearfix">
<blockquote>“<?php the_field( 'testimonial' ); ?>”</blockquote>
<cite>—<?php the_field( 'name' ); ?></cite>
</div>
<?php endwhile; else : ?>
<p>Non ci sono testimonianze :( </p>
<?php endif;
wp_reset_postdata(); ?>
E inoltre non dimenticare di resettare i postdata se stai eseguendo multiple query su una pagina.

Fantastico! Grazie mille amico! Che seccatura, mi è sfuggita la cosa più semplice. Ha funzionato alla perfezione!

Assicurati di resettare anche i postdata per ogni query, se stai eseguendo più query su una pagina. Con wp_reset_postdata(); Ho aggiornato la mia risposta con questa informazione.

@JakeGabb non dimenticare di accettare questa risposta :-). Per maggiori informazioni, dai un'occhiata alla pagina [tour]

@JakeGabb ah sì, restrizioni per nuovi utenti, mi ero dimenticato di quello, scusa. BTW Benvenuto su WPSE.
