Nous avons un type de message personnalisé testimonial
et voulons afficher les 1er, 2e et 3e points à divers endroits sur la page.
Voici comment nous le ferions si nous voulions simplement lire en boucle des témoignages et les afficher l'un après l'autre (cela fonctionne):
<?php
$args = array('post_type' => 'testimonial');
$testimonials = new WP_Query( $args );
while( $testimonials->have_posts() ) {
$testimonials->the_post();
?>
<li>
<?php echo get_the_content(); ?>
</li>
<?php }?>
Mais ne montrer que le premier ou seulement le troisième ou le nième? Voici ce que nous avons jusqu'à présent (pour obtenir le premier), c'est évidemment faux:
$args = array('post_type' => 'testimonial');
$testimonials = new WP_Query( $args );
$testimonial1 =$testimonials[0]->the_post;
echo $testimonial1->the_content;
Nous obtenons une erreur en disant:
Erreur fatale: impossible d'utiliser un objet de type WP_Query en tant que tableau
Utilisez un décalage pour ignorer les 2 premiers articles si vous avez besoin du troisième article uniquement, puis définissez votre posts_per_page
sur 1
pour obtenir uniquement cet article spécifique.
Vous pouvez essayer quelque chose comme ça dans vos arguments
$args = array(
'post_type' => 'testimonial',
'offset' => 2,
'posts_per_page' => 1
);
$testimonials = new WP_Query( $args );
while( $testimonials->have_posts() ) {
$testimonials->the_post();
?>
<li>
<?php the_content(); ?>
</li>
<?php }
wp_reset_postdata(); ?>
Avant de commencer, quelques notes sur votre code et mon code modifié. ( J'ai édité mon code d'origine pour afficher une boucle normale ).
Vous n'avez pas besoin d'utiliser echo get_the_content()
. Vous pouvez simplement utiliser the_content()
directement
N'oubliez pas de réinitialiser vos données postérieures après la boucle avec wp_reset_postdata()
.
Comme demandé dans les commentaires, voici la syntaxe alternative dans laquelle vous n'utilisez pas la boucle. En outre, une note ou deux en premier:
Vous devez travailler directement avec les objets WP_Post
et utiliser les filtres décrits dans le lien link
Tu peux essayer
$args = array(
'post_type' => 'testimonial',
'offset' => 2,
'posts_per_page' => 1
);
$testimonials = new WP_Query( $args );
//Displays the title
echo apply_filters( 'the_title', $testimonials->posts[0]->post_title );
//Displays the content
echo apply_filters( 'the_content', $testimonials->posts[0]->post_content );
Vous pouvez utiliser la propriété current_post
et la méthode rewind_posts()
de la classe WP_Query
pour cibler les objets de publication pertinents.
Vous pouvez essayer de jouer avec l'exemple suivant (non testé):
if( $testimonials->post_count >= 3 )
{
// First post:
$nr = 1;
if( isset( $testimonials->posts[$nr-1] ) )
{
$testimonials->rewind_posts();
$testimonials->current_post = $nr - 2;
$testimonials->the_post();
the_title();
}
// Third post:
$nr = 3;
if( isset( $testimonials->posts[$nr-1] ) )
{
$testimonials->rewind_posts();
$testimonials->current_post = $nr - 2;
$testimonials->the_post();
the_title();
}
// Second post:
$nr = 2;
if( isset( $testimonials->posts[$nr-1] ) )
{
$testimonials->rewind_posts();
$testimonials->current_post = $nr - 2;
$testimonials->the_post();
the_title();
}
wp_reset_postdata();
}
Vous pouvez également l'utiliser comme ceci, avec vos boucles while
actuelles:
$testimonials = new WP_Query( $args );
// First post:
$nr = 1;
while( $testimonials->have_posts() && ( $nr - 2 ) === $testimonials->current_post ) {
$testimonials->the_post();
// ...
}
// Third post:
$nr = 3;
$testimonials->rewind_posts();
while( $testimonials->have_posts() && ( $nr - 2 ) === $testimonials->current_post ) {
$testimonials->the_post();
// ...
}
// Second post:
$nr = 2;
$testimonials->rewind_posts();
while( $testimonials->have_posts() && ( $nr - 2 ) === $testimonials->current_post ) {
$testimonials->the_post();
// ...
}
wp_reset_postdata();