Afficher article "sticky post" en priorité sur les pages

Par Florian.C, il y a 9 ans


Bonjour,

Voila je rencontre un petit problème avec mon code. J'utilise wordpress, oui encore moi. :)

J'ai 4 articles qui sont affichés mais j'aimerai que lorsque l'utilisateur à cocher la case "mettre en avant" il prenne alors la priorité sur les autres dans ma page (et non la homepage.

Décrivez ici votre code ou ce que vous cherchez à faire

Pour commencer voici mon code

$args = array( 'post_type' => 'post', 'post_status' => 'publish', 'category_name' => 'conferences', 'posts_per_page' => 4, 'order' => 'DESC', ); $query1 = new WP_Query($args); if ( $query1->have_posts() ) { // The Loop while ( $query1->have_posts() ) { $query1->the_post(); ?> <div class="agenda-une-container"> <?php $titre = get_the_title(); $date = get_post_meta($post->ID, '_data_evenement', true); $etablissement = wp_get_post_terms($post->ID, 'etablissement', array("fields" => "all")); $theme = wp_get_post_terms($post->ID, 'theme', array("fields" => "all")); $region = wp_get_post_terms($post->ID, 'region', array("fields" => "all")); ?> <p><a href="<?= get_permalink($post->ID) ?>"><?= $titre ?></a></p> <p><?php print_r(date('d/m/Y',$date)); ?></p> <p>Le lieu : <?php echo $etablissement[0]->name ; ?></p> <p>Le thème sera : <?php echo $theme[0]->name ; ?></p> <p>La région : <?php echo $region[0]->name ; ?></p> --------- </div> <?php } /* * Restore original Post Data */ wp_reset_postdata(); }

Je recherche donc des pistes pour faire en sorte que lorsque un article "is_sticky" il prenne la priorité dans ma fonction wp_query.
J'ai lu le codex de wordpress mais je n'arrive pas malgré tout à le faire fonctionner. (https://codex.wordpress.org/Sticky_Posts)

J'espère être clair dans mes explications ! :) Merci d'avance

PS : Bonne fêtes

16 réponses

Florian.C, il y a 9 ans

Y'a des gens sur le forum wordpress ?

Sleeping, il y a 9 ans

Salut,
Tente ceci :

$args = array( 'post_type' => 'post', 'post_status' => 'publish', 'category_name' => 'conferences', 'posts_per_page' => 4, 'order' => 'DESC', 'ignore_sticky_posts' => 0, );
Florian.C, il y a 9 ans

Je vais tester, merci !

Florian.C, il y a 9 ans

L'article est bien mis en avant sur la page d'accueil mais pas sur ma page agenda :/

Sleeping, il y a 9 ans

Tu utilise quel requete pour ta page agenda ?

Florian.C, il y a 9 ans

Tout ce que tu vois plus haut et le reste du code c'est page.php

Florian.C, il y a 9 ans

@Sleeping, désolé, je t'ai répondu mais pas toi ! Merci beaucoup bonne soirée :D

Sleeping, il y a 9 ans

Hey !

Oublie pas de marquer la réponse en résolu :)

Florian.C, il y a 9 ans

J'aimerai bien, mais ligne n'a pas résolu le probleme ! :/

Sleeping, il y a 9 ans

Oups mal lu, my bad :P

Utilise tu une page spécial pour l'agenda?

Florian.C, il y a 9 ans

Aucun soucis, encore une fois merci de m'aider !

Oui c'est un template de page que j'ai créé

Florian.C, il y a 9 ans

Le template-agenda

Sleeping, il y a 9 ans

Peux-tu me mettre la template ici ?

Florian.C, il y a 9 ans

Bien sur ! :)

<?php /* Template Name: Agenda */ ?> <?php get_header(); ?> <script src="assets/js/florian-script.js"></script> <div class="wrap"> <div id="primary" class="content-area"> <main id="main" class="site-main" role="main"> <h2 style="float:left">A la une</h2> <div class="filtres"> <?php $d = ""; if( isset( $_GET['d'] ) ) $d = intval( $_GET['d'] ); //recherche tout les termes de la taxonomy region $regions = get_terms( 'region', array( 'hide_empty' => true ) ); echo '<label for="filtre-region">Filtrer par régions : </label>'; echo '<select id="filtre-region">'; echo '<option'; if( $d == "" ) echo ' selected="selected"'; echo ' value="">Tous les régions</option>'; foreach ($regions as $region) { echo '<option'; if( $d == $region->term_id ) echo ' selected="selected"'; echo ' value="' . $region->term_id . '">' . $region->name . '</option>'; } echo '</select>'; ?> </div> <h1 style="clear: both; padding-top: 5%;">le caroussel</h1> <!-- LES CONFERENCES A LA UNE--> <div class="conf-une"> <h4>Les conférences à la Une</h4> <?php $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'category_name' => 'conferences', 'posts_per_page' => 4, 'order' => 'DESC', 'ignore_sticky_posts' => 0, ); $query1 = new WP_Query($args); // The Query if ( $query1->have_posts() ) { // The Loop while ( $query1->have_posts() ) { $query1->the_post(); ?> <div class="agenda-une-container"> <?php $titre = get_the_title(); $date = get_post_meta($post->ID, '_data_evenement', true); $etablissement = wp_get_post_terms($post->ID, 'etablissement', array("fields" => "all")); $theme = wp_get_post_terms($post->ID, 'theme', array("fields" => "all")); $region = wp_get_post_terms($post->ID, 'region', array("fields" => "all")); ?> <p><a href="<?= get_permalink($post->ID) ?>"><?= $titre ?></a></p> <p><?php print_r(date('d/m/Y',$date)); ?></p> <p>Le lieu : <?php echo $etablissement[0]->name ; ?></p> <p>Le thème sera : <?php echo $theme[0]->name ; ?></p> <p>La région : <?php echo $region[0]->name ; ?></p> --------- </div> <?php } /* * Restore original Post Data */ wp_reset_postdata(); } ?> </div> <!-- LES CONFERENCES A LA UNE--> <div class="conf-une"> <h4>Les Tables Rondes à la Une</h4> <?php $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'category_name' => 'tables-rondes', 'posts_per_page' => 4, 'order' => 'DESC', ); $query1 = new WP_Query($args); // The Query if ( $query1->have_posts() ) { // The Loop while ( $query1->have_posts() ) { $query1->the_post(); ?> <div class="agenda-une-container"> <?php $titre = get_the_title(); $date = get_post_meta($post->ID, '_data_evenement', true); $etablissement = wp_get_post_terms($post->ID, 'etablissement', array("fields" => "all")); $theme = wp_get_post_terms($post->ID, 'theme', array("fields" => "all")); $region = wp_get_post_terms($post->ID, 'region', array("fields" => "all")); ?> <p><a href="<?= get_permalink($post->ID) ?>"><?= $titre ?></a></p> <p><?php print_r(date('d/m/Y',$date)); ?></p> <p>Le lieu : <?php echo $etablissement[0]->name ; ?></p> <p>Le thème sera : <?php echo $theme[0]->name ; ?></p> <p>La région : <?php echo $region[0]->name ; ?></p> --------- </div> <?php } /* * Restore original Post Data */ wp_reset_postdata(); } ?> </div> <!-- LES CONFERENCES A LA UNE--> <div class="conf-une"> <h4>Les Events à la Une</h4> <?php $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'category_name' => 'events', 'posts_per_page' => 4, 'order' => 'DESC', ); $query1 = new WP_Query($args); // The Query if ( $query1->have_posts() ) { // The Loop while ( $query1->have_posts() ) { $query1->the_post(); ?> <div class="agenda-une-container"> <?php $titre = get_the_title(); $date = get_post_meta($post->ID, '_data_evenement', true); $etablissement = wp_get_post_terms($post->ID, 'etablissement', array("fields" => "all")); $theme = wp_get_post_terms($post->ID, 'theme', array("fields" => "all")); $region = wp_get_post_terms($post->ID, 'region', array("fields" => "all")); ?> <p><a href="<?= get_permalink($post->ID) ?>"><?= $titre ?></a></p> <p><?php print_r(date('d/m/Y',$date)); ?></p> <p>Le lieu : <?php echo $etablissement[0]->name ; ?></p> <p>Le thème sera : <?php echo $theme[0]->name ; ?></p> <p>La région : <?php echo $region[0]->name ; ?></p> --------- </div> <?php } /* * Restore original Post Data */ wp_reset_postdata(); } ?> </div> </main><!-- #main --> </div><!-- #primary --> </div><!-- .wrap --> <?php get_footer();
Florian.C, il y a 9 ans

Merci beaucoup Guique ;)

Guique, il y a 9 ans

Hello,

Tes sticky posts sont stockés dans l'option "sticky posts" de WordPress.

Pour les récupérer :

$sticky = get_option( 'sticky_posts' );

Tu vas récupérer un tableau avec tes sticky triés du plus ancien au plus récent. Tu inverse donc ton tableau :

rsort( $sticky );

Ensuite tu coupe ton tableau pour ne garder que les 3 derniers par exemple :

$sticky = array_slice( $sticky, 0, 3 );

Tu as maintenant un tableau avec tes 3 derniers sticky.
Tu peux t'en servir pour customiser tes boucles.

WordPress, donnes moi mes 3 derniers sticky stp :

$query = new WP_Query( array( 'post_type' => 'post', 'post__in' => $sticky ) );

WordPress, donnes moi 3 articles qui ne sont pas sticky stp :

$query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => $sticky, 'posts_per_page' => 3 ) );