WP_Query non mostra post type personalizzato, loop multipli e get_template_part

28 ago 2014, 14:16:39
Visualizzazioni: 17K
Voti: 0

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>&ldquo;<?php the_field( 'testimonial' ); ?>&rdquo;</blockquote>
        <cite>&mdash;<?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!

0
Tutte le risposte alla domanda 1
6

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>&ldquo;<?php the_field( 'testimonial' ); ?>&rdquo;</blockquote>
        <cite>&mdash;<?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.

28 ago 2014 14:20:36
Commenti

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

Jake Gabb Jake Gabb
28 ago 2014 14:22:49

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.

Robert hue Robert hue
28 ago 2014 14:24:07

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

Pieter Goosen Pieter Goosen
28 ago 2014 14:24:46

Non ho dimenticato ;), ma diceva che devo aspettare 9 minuti :'(

Jake Gabb Jake Gabb
28 ago 2014 14:25:56

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

Pieter Goosen Pieter Goosen
28 ago 2014 14:48:10

Benvenuto su WPSE @JakeGabb ;)

Robert hue Robert hue
28 ago 2014 14:59:17
Mostra i restanti 1 commenti