繁体   English   中英

按月份显示帖子(带有月份标题)-Wordpress

[英]Display posts by month (with month title) - Wordpress

我想实现这样的目标,我不知道是否有可能,什么是最好的方法:

http://oi60.tinypic.com/21dokqu.jpg

我查询帖子的方式是这样的:

<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>

任何人都可以给我提示如何做或最好的方法?

这是我尝试过的,但未显示任何内容:

global $wpdb; // Don't forget

$collection = $wpdb->get_results("
  SELECT YEAR(p.post_date) AS post_year, MONTH(p.post_date) AS post_month, p.*
  FROM {$wpdb->posts} AS p
  WHERE p.post_type = 'post' AND p.post_status = 'publish'
  ORDER BY p.post_date DESC
", OBJECT );

// Loop once to grab the years
foreach ( $collection as $year ):

  // Loop for a second time to grab the months inside a year
  foreach ( $collection as $month ):

    // Continue unless years match
    if ( $month->post_year != $year ) continue;

    // Loop one more time to grab the posts inside the month, inside the year
    foreach ( $collection as $post ):

      // Continue unless months and years match
      if ( $post->post_year != $year || $post->post_month != $month ) continue;

      // You can use the posts data here

    endforeach;

  endforeach;

endforeach; 

希望这会有所帮助:

$args = array(
            'post_type' => 'lecturas-post',
            'numberposts' => 15,
            'orderby' => 'post_date',
            'order' => 'DESC',
        );

$posts = wp_get_recent_posts( $args );
$collection = array();

foreach ( $posts as $lectura ){
    $index = date( 'Y-m', strtotime($lectura['post_date']) );
    $collection[ $index ][] = $lectura;
}

$ collection现在包含每月的帖子,您可以轻松地循环浏览它们。

试试这个,我希望这对你有用。

$args = array('posts_per_page' => -1, 'orderby' => 'date' );

$myQuery = new WP_Query($args);

$date = '';

if ( $myQuery->have_posts() ) : while ( $myQuery->have_posts() ) : $myQuery->the_post();

if ( $date != get_the_date() ) {
    echo $date;
    echo '<hr />';
    $date = get_the_date();
}

the_title(); // or whatever you want here.
echo '<br />';

endwhile; endif;
wp_reset_postdata();

有关查询的更多信息,请访问: http : //codex.wordpress.org/Class_Reference/WP_Query

我从这里找到答案。 在Wordpress中按日期对帖子进行分组

mikevoermans给出的答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM