web-dev-qa-db-fra.com

Afficher les messages par mois

Je veux réaliser quelque chose comme ça, je ne sais pas si c'est possible et quelle serait la meilleure façon de le faire:

enter image description here

J'interroge les messages de la manière suivante:

<div class="post">
    <?php global $wp_query;
    query_posts( array('post_type' => array( 'lecturas-post' ),'showposts' => 15, 'paged'=>$paged, 'order' => 'DESC' ));?>
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
        <div><?php the_title() ?></a>
    <?php endwhile; // end of the loop. ?>
</div>

Tout le monde peut me donner un conseil sur la meilleure façon de procéder?

3
wpuser

Comme indiqué dans un commentaire, vous pouvez le faire en une seule requête. Le principe ici est d'afficher uniquement l'en-tête de la date si le mois de la date de publication de la publication précédente ne correspond pas à celui de la publication précédente.

Quelques notes

Avant de commencer, quelques notes:

  • Jamais utilisez query_posts, sauf si vous avez vraiment besoin de tout casser sur une page. Il ne se contente pas de relancer la requête principale et de la casser, il gâche la pagination et vos paramètres globaux, ainsi que les fonctions d'objet demandées. Si vous devez vraiment exécuter une requête personnalisée, utilisez WP_Query ou get_posts

  • showposts a été abandonné il y a des années au profit de posts_per_page

  • Pas besoin d'utiliser le $wp_query global. query_posts ça déconne quand même

LE PLAN

Les publications sont affichées dans l'ordre chronologique, la publication la plus récente en premier et la publication la plus ancienne en dernier, elles sont donc déjà dans le bon ordre. Il s’agit simplement d’afficher la date à l’endroit approprié.

Pour ce faire, tout ce que vous avez à faire est d’obtenir le mois et l’année en cours à partir de la date de publication du message en cours, puis de le comparer au mois de la date de publication de la publication précédente, puis d’afficher la date si les mois ne correspondent pas ou non. l'afficher si elles correspondent

Comme explication, je vais utiliser la boucle principale avec la requête principale.

Pour ce faire, vous devez:

  • Obtenez le mois à partir de la date de publication de l'article en cours. Pour ce faire, utilisez get_the_date( 'F' )

  • Obtenez le post précédent dans la boucle avec $wp_query->posts['this will be current post -1 ']->post.

  • Obtenez et comparez les mois entre les deux posts

  • Afficher ou ne pas afficher la date en fonction de la comparaison

LE CODE

Ce code va dans votre boucle, juste après votre déclaration while().

$current_month = get_the_date('F');

if( $wp_query->current_post === 0 ) { 

   the_date( 'F Y' );

}else{ 

    $f = $wp_query->current_post - 1;       
    $old_date =   mysql2date( 'F', $wp_query->posts[$f]->post_date ); 

    if($current_month != $old_date) {

        the_date( 'F Y' );;

    }

}

Requête personnalisée

Si vous devez exécuter une requête personnalisée, essayez ceci.

$q = new WP_Query( array('post_type' => array( 'lecturas-post' ),'posts_per_page' => 15, 'paged'=>$paged, 'order' => 'DESC' ));

if( $q->have_posts() ) {
    while( $q->have_posts() ) {

        $q->the_post();

        $current_month = get_the_date('F');

        if( $q->current_post === 0 ) { 

           the_date( 'F Y' );

        }else{ 

            $f = $q->current_post - 1;       
            $old_date =   mysql2date( 'F', $q->posts[$f]->post_date ); 

            if($current_month != $old_date) {

                the_date( 'F Y' );;

            }

        }

        the_title();

    }

}
9
Pieter Goosen

Réponse mise à jour

Après avoir réfléchi au commentaire de @PieterGoosen ci-dessous, j'ai ajouté une méthode pour atteindre votre objectif en utilisant une requête unique.

J'ai analysé les méthodes et la requête simple est plus rapide, la méthode de requête multiple étant environ 15% plus lente . Pas une marge énorme, mais chaque petite aide, et pour être honnête, la méthode peut probablement être affinée davantage.

J'ai laissé la méthode de requête multiple dans la réponse à la prospérité, mais je recommande d'utiliser la méthode de requête unique.

Méthode de requête unique

$time_start = microtime(true);

/** Set up a date interval object for 6 monts ago (you can change as required) */
$interval = new DateInterval('P6M');
$interval->invert = 1;

/** Grab the date as it was 6 months ago */
$date = new DateTime(date('Y-m-d'));
$date->add($interval);

/** Query the database for all posts newer than the the given date interval */
$args = array(
    'nopaging'          => true,
    'posts_per_page'    => -1,
    'post_type'         => 'post',
    'post_status'       => 'publish',
    'order_by'          => 'date',
    'date_query'        => array(
        'after' => $date->format('Y-m-d')
    )
);
$month_query = new WP_Query($args);

/** Check to ensure that there are articles for this month... */
if($month_query->have_posts()) :

    $month_titles = array();
    $close_ul = false;


    //echo '<ul style="padding-left: 250px;" id="monthly-posts">';

    /** Set the attributes for displaying the title as an attribute */
    $title_attribute_args = array(
        'before'    => 'Visit article \'',
        'after'     => '\' ',
        'echo'      => false
    );      

    /** Loop through each post for this month... */
    while($month_query->have_posts()) : $month_query->the_post();

        /** Check the month/year of the current post */
        $month_title = date('F Y', strtotime(get_the_date('Y-m-d H:i:s')));

        /** Maybe output a human friendly date, if it's not already been output */
        if(!in_array($month_title, $month_titles)) :

            if($close_ul) echo '</ul>';                                                             // Check if the unordered list of posts should be closed (it shouldn't for the first '$monthe_title')
            echo '<h1 style="padding-left: 250px;" id="monthly-title">' . $month_title . '</h1>';   // Output the '$month_title'
            echo '<ul style="padding-left: 250px;" id="monthly-posts">';                            // Open an unordered lists for the posts that are to come
            $month_titles[] = $month_title;                                                         // Add this `$month_title' to the array of `$month_titles` so that it is not repeated
            $close_ul = true;                                                                       // Indicate that the unordered list should be closed at the next oppurtunity

        endif;

        /** Output each article for this month */
        printf(
            '<li id="monthly-post-%1$s">%2$s <a href="%3$s" title="%4$s">%3$s</a></li>',
            get_the_ID(),                               /** %1$s - The ID of the post */
            get_the_title(),                            /** %2$s - The article title */
            get_permalink(get_the_ID()),                /** %3$s - The article link */
            the_title_attribute($title_attribute_args)  /** %4$s - The title for use as an attribute */
        );

    endwhile;

    if($close_ul) echo '</ul>'; // Close the last unordered list of posts (if there are any shown)

endif;

/** Reset the query so that WP doesn't do funky stuff */
wp_reset_query();

Réponse originale

Essayez ceci. Je l'ai configuré pour que seuls les 6 derniers mois soient choisis et que les 5 derniers messages de chaque mois, mais vous pouvez modifier cela à votre guise.

Essentiellement, le code vérifie d'abord quels mois contiennent des publications, puis affiche les cinq dernières publications de ce mois, ainsi qu'un lien.

Méthode de requête multiple

global $wpdb, $wp_locale;

/** Query the individual months to display (I've chosen the last 6 months) */
$query = $wpdb->prepare('
    SELECT DISTINCT YEAR(`%1$s`.`post_date`) AS year, MONTH(`%1$s`.`post_date`) AS month
    FROM `%1$s`
    WHERE `%1$s`.`post_type` = "post"
    ORDER BY `%1$s`.`post_date` DESC
    LIMIT 6',
    $wpdb->posts
);
$months = $wpdb->get_results($query);

/** Count the number of months */
$month_count = count($months);

/** Ensure that there are months to display... */
if($month_count || ($month_count === 1 && $months[0]->month > 0)) :

    /** Loop through each month... */
    foreach($months as $month) :

        if($month->year === 0) :
            continue;
        endif;

        /** Grab the individual month and year, and construct a human friendly date (for the title) */
        $m = zeroise($month->month, 2);
        $y = $month->year;
        $human_date = sprintf(__('%1$s %2$d'), $wp_locale->get_month($m), $y);

        /** Grab any posts for this month (I've chosedn only the last 5 posts) */
        $args = array(
            'nopaging'          => true,
            'posts_per_page'    => 5,
            'post_type'         => 'post',
            'post_status'       => 'publish',
            'order_by'          => 'date',
            'year'              => $y,
            'monthnum'          => $m
        );
        $month_query = new WP_Query($args);

        /** Check to ensure that there are articles for this month... */
        if($month_query->have_posts()) :

            /** Output a human friendly date */
            echo '<h1 id="monthly-title">' . $human_date . '</h1>';
            echo '<ul id="monthly-posts">';

            /** Set the attributes for displaying the title as an attribute */
            $title_attribute_args = array(
                'before'    => 'Visit article \'',
                'after'     => '\' ',
                'echo'      => false
            );      

            /** Loop through each post for this month... */
            while($month_query->have_posts()) : $month_query->the_post();

                /** Output each article for this month */
                printf(
                    '<li id="monthly-post-%1$s">%2$s <a href="%3$s" title="%4$s">%3$s</a></li>',
                    get_the_ID(),                               /** %1$s - The ID of the post */
                    get_the_title(),                            /** %2$s - The article title */
                    get_permalink(get_the_ID()),                /** %3$s - The article link */
                    the_title_attribute($title_attribute_args)  /** %4$s - The title for use as an attribute */
                );

            endwhile;

            echo '</ul>';

        endif;

        /** Reset the query so that WP doesn't do funky stuff */
        wp_reset_query();

    endforeach;

endif;
4
David Gard

C’est une fonction que j’ai créée pour utiliser pour des besoins généraux, pour récupérer des données de type publication ou personnalisation avant ou après une année/un mois ou l’année en cours, dans l’ordre suivant:

// you could change the name in case it collides with some other plugin
function get_posts_by_date($user_options = array()){

  $options = array(
    'year_limit' => '1980'
    ,'month_limit' => '01'
    ,'operator' => '>=' // date comparison operator
    ,'current_year' => true // limit data to current year
    ,'post_type' => 'post'
    ,'year_order' => 'DESC'
    ,'month_order' => 'DESC'
    ,'post_ids_order' => 'DESC'
    ,'raw_output' => false
  );

  extract(array_merge($options, $user_options));

  global $wpdb;

  if($operator == '>=' || $operator == '=='){
    $day = "01";
  } elseif($mode == '<='){
    $day = "31";
  }

  if($current_year){ // will be after [previous year]/12/31
    $year_limit = date('Y', strtotime('-1 year'));
    $month_limit = '12';
    $day = "31";
    $operator == '>=';
  }

  // warning: if your parameters come from user input/forms, 
  // pass them using $wpdb::prepare()
  // https://developer.wordpress.org/reference/classes/wpdb/prepare/
  $results = $wpdb->get_results("
    SELECT tbl.y year, group_concat(month_posts ORDER BY tbl.m " . $month_order . " SEPARATOR '-') months
      FROM (
        SELECT YEAR(p.post_date) y, MONTH(p.post_date) m, concat(MONTH(p.post_date), ':', group_concat(p.id ORDER BY p.post_date " . $post_ids_order . " )) month_posts
        FROM $wpdb->posts p
        WHERE (p.post_status = 'publish' OR p.post_status = 'future')
          AND p.post_type = '" . $post_type . "'
          AND p.post_date " . $operator . " DATE('" . $year_limit . "-" . $month_limit . "-" . $day . " 00:00:00')
        GROUP BY y, m
      ) tbl
    GROUP BY tbl.y
    ORDER BY tbl.y " . $year_order
  );

  if($raw_output) return $results;

  global $wp_locale;

  foreach ($results as $data){
    $months_data = explode('-',$data->months);
    $months = array();
    $data->count = 0; // year count

    foreach ($months_data as $month_data){
      $month_obj = new stdClass;

      $splitted = explode(':',$month_data);
      $raw_month = $splitted[0];
      $month_obj->number = $raw_month;
      $month_obj->name = $wp_locale->get_month($raw_month);
      $month_obj->posts = array();
      $post_ids = explode(',',$splitted[1]);
      $data->count += count($post_ids);

      foreach($post_ids as $post_id){
        $month_obj->posts[] = get_post($post_id);
        $months[$raw_month] = $month_obj;
      }// foreach
    }// foreach

    $data->months = $months;
  }// foreach

  return $results;
}// get_posts_by_date

Exemple d'utilisation:

$posts_by_date = get_posts_by_date(array(
  'year_limit' => '2016'
  ,'operator' => '<='
  ,'current_year' => false
  ,'post_type' => 'product'
  ,'month_order' => 'ASC'
  ,'raw_output' => true
));

Si l'option raw_output est vraie, le résultat par défaut sera le suivant:

array(2) {
  [0]=>
  object(stdClass)#6645 (2) {
    ["year"]=>
    string(4) "2017"
    ["months"]=>
    string(65) "8:386,401-7:406,373,380,377,408,399,362-6:1,391,404-5:367,397,394"
  }
  [1]=>
  object(stdClass)#6644 (2) {
    ["year"]=>
    string(4) "2016"
    ["months"]=>
    string(5) "6:429"
  }
}

La chaîne "months" contient des valeurs au format:

month:[post ids]-month:[post ids]-ecc

Si l'option raw_output est false, vous obtenez une liste de publications comme celle-ci:

array (array of objects)
  object
    -> year (ex. '2017')
    -> count (total year's posts)
    -> months (array of objects)
        month
          -> number (of month)
          -> name (localized)
          -> posts (array of post objects)

Bonne codage ... :)

1
Luca Reghellin