La boucle WP
Wordpress génère en fonction du contexte une variable globale de type tableau $posts.
Ce tableau contient selon le menu sélectionné la ou les pages, l'article ou les articles contenus dans la base de données.
Ce que l'on appelle la boucle WordPress est un while qui fait défiler ce tableau.
Chaque élément de ce tableau est un objet $post (class WP_Post).
Méthode : $posts
Modifiez le fichier index.php
Testez en cliquant sur les différents liens de votre menu.
Méthode : $post
Modifiez le fichier index.php
get_header();
<main>
if (have_posts()) : while (have_posts()) :
the_post();
<article>
var_dump($post);
</article>
endwhile;
endif;
</main>
get_sidebar();
get_footer();
<?php get_header(); ?> <main> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <article> <?php var_dump($post); ?> </article> <?php endwhile; endif; ?> </main> <?php get_sidebar(); ?> <?php get_footer(); ?>
have_posts() : vérifie que $posts n'est pas vide.
the_post() : met à jour $post avec le post suivant.
while(...) : ... endwhile() ; : WP utilise plus volontiers l'écriture : que {}
Testez en cliquant sur les différents liens de votre menu.
Définition : Les fonctions
Il existe différentes fonctions qui permettent d'afficher facilement et proprement le contenu de $post :
the_ID(); // affiche l'id
the_title(); // affiche le titre
the_author(); // affiche l'auteur
the_content(); // affiche le contenu
the_excerpt(); // affiche l'extrait
the_permalink(); // affiche l'url du lien
the_post_thumbnail(); // affiche l'image à la Une
the_time(); // affiche la date
the_category(); // affiche la ou les catégories sous forme de liens
the_tags(); // affiche les étiquettes sous forme de liens
<?php the_ID(); // affiche l'id the_title(); // affiche le titre the_author(); // affiche l'auteur the_content(); // affiche le contenu the_excerpt(); // affiche l'extrait the_permalink(); // affiche l'url du lien the_post_thumbnail(); // affiche l'image à la Une the_time(); // affiche la date the_category(); // affiche la ou les catégories sous forme de liens the_tags(); // affiche les étiquettes sous forme de liens
Il existe différentes fonctions qui permettent de récupérer facilement et proprement le contenu de $post :
get_the_ID(); // récupère l'id
get_the_title(); // récupère le titre
get_the_author(); // récupère l'auteur
get_the_content(); // récupère le contenu
get_the_excerpt(); // récupère l'extrait
get_permalink(); // récupère l'url du lien
get_the_post_thumbnail(); // récupère l'image à la Une
get_the_time(); // récupère la date
get_the_category(); // récupère la ou les catégories sous forme de liens
get_the_tags(); // récupère les étiquettes sous forme de liens
<?php get_the_ID(); // récupère l'id get_the_title(); // récupère le titre get_the_author(); // récupère l'auteur get_the_content(); // récupère le contenu get_the_excerpt(); // récupère l'extrait get_permalink(); // récupère l'url du lien get_the_post_thumbnail(); // récupère l'image à la Une get_the_time(); // récupère la date get_the_category(); // récupère la ou les catégories sous forme de liens get_the_tags(); // récupère les étiquettes sous forme de liens
Pour d'autres fonctions voir le codex -> https://codex.wordpress.org/fr:Accueil
Méthode : Exemple
Modifiez le fichier index.php
get_header();
<main>
if (have_posts()) : while (have_posts()) :
the_post();
<article>
the_title('<h2><a href="' .
esc_url(get_permalink()) . '">', '</a></h2>');
the_content();
</article>
endwhile;
endif;
</main>
get_sidebar();
get_footer();
<?php get_header(); ?> <main> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <article> <?php the_title('<h2><a href="' . esc_url(get_permalink()) . '">', '</a></h2>'); ?> <?php the_content(); ?> </article> <?php endwhile; endif; ?> </main> <?php get_sidebar(); ?> <?php get_footer(); ?>