【问题标题】:get wordpress posts grouped by month获取按月分组的 wordpress 帖子
【发布时间】:2013-09-27 12:02:07
【问题描述】:

我想在一个页面上显示所有 Wordpress 帖子,并让结果显示如下:

9 月(当月)的帖子

1- 第一篇文章 2-秒发帖 3- 第三个帖子

下个月的帖子

2- 第一篇文章 2-秒发帖 3- 第三个帖子

我已经完成了这个结果来显示按月排序的所有帖子,但我无法对它们进行分组并将帖子显示为每个月的一个组:

 <?php
$month = date('M');
$arrgs = array('orderby' => $month);
$myposts = get_posts($arrgs);
foreach($myposts as $post) :
setup_postdata($post);
?>
<div class="post-item">
    <div class="post-info">
        <h2 class="post-title">
            <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
                <?php the_title(); ?>
            </a>
        </h2>
        <p class="post-meta">Posted by <?php the_author(); ?> Published in <strong><?php echo $month; ?></strong></p>
    </div>
</div>
<?php endforeach; wp_reset_postdata(); ?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    @zillaofthegods 这是我的解决方案:我只使用 1 个查询来获取我想要显示的所有帖子,然后使用 PHP 来完成繁重的工作。一个好主意是要求将年份和月份作为额外的列,因为我们稍后会需要它们。

    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;
    

    有什么好处?您只需点击一次数据库,其他一切都由 PHP 作为模板渲染的一部分完成,可以轻松缓存。

    【讨论】:

    • 支持更有效的解决方案。感谢您接受我的请求!
    【解决方案2】:

    所以,我不确定是否有其他方法,但我必须对数据库进行一些直接查询才能实现您要查找的内容。这个例子在很多类似的场合都对我有用。

    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>               
     <ul>
      <?php
       $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE  post_status = 'publish' ORDER BY post_date ASC");
    
       foreach($years as $year) : ?>
    
        <h4><li><a href="<?php echo get_year_link($year ); ?> "><?php echo $year; ?></a>
         <ul>
          <?php $months = $wpdb->get_col("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_status = 'publish' AND YEAR(post_date) = '".$year."' ORDER BY post_date ASC");
    
          foreach($months as $month) : ?>
    
           <h4>
            <li>
             <a href="<?php echo get_month_link($year, $month); ?>"><?php echo date( 'F', mktime(0, 0, 0, $month) );?></a>
             <ul>
              <?php  $theids = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND MONTH(post_date)= '".$month."' AND YEAR(post_date) = '".$year."' ORDER BY post_date ASC");
    
               foreach ($theids as $theid): ?>
    
                <h4 style= font-style:italic;><li><a href="<?php bloginfo('url'); ?>?p=<?php echo $theid->ID; ?>"><?php echo $theid->post_title; ?></a></li></h4>  
    
               <?php endforeach; ?>
    
             </ul>                
            </li></h4>
    
           <?php endforeach;?>
    
          </ul>
         </li></h4>
    
        <?php endforeach; ?>
    
       </ul>
      </div>
    </div>
    

    还有this 插件。它工作得很好,但定制螨的水平不在同一水平。但它确实有效,并且将是解决您的情况的最快和最简单的解决方案。

    编码愉快!

    【讨论】:

    • 拒绝在循环中使用多个查询。那是错误的。
    • @iKindred,是否愿意提供更充分的解决方案?
    猜你喜欢
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多