【问题标题】:Archive Template in WordpressWordpress 中的存档模板
【发布时间】:2010-11-16 11:53:09
【问题描述】:

我想为 Wordpress 创建一个存档页面模板,如下所示:

2009 年 8 月

  • 发布 4
  • 发布 3
  • 发布 2
  • 发布 1

2009 年 7 月

  • 发布 2
  • 发布 1

所以,基本上,我想要博客中的所有帖子,按日期降序排列并按月份分组。 有人可以提供我的 PHP 代码吗?

谢谢!

PS:Wordpress 版本是 2.8.2

【问题讨论】:

    标签: php wordpress templates


    【解决方案1】:

    非常感谢您的帮助。这就是我在上面的代码中使用的。

    function condensed_post_list($order='DESC', $date_prefix=true, $display=true)
    {
        if( !in_array($order, array('DESC','ASC' ) ) ) $order = 'DESC';
        $args = array(
            'numberposts'     => -1,
            'orderby'         => 'post_date',
            'post_type'       => 'post',
            'post_status'     => 'publish');
        $results = get_posts($args);
    
        ob_start();
        $current_month = '';
        foreach( $results as $result ) {
            if( $current_month != mysql2date('F Y', $result->post_date)) {
                if( $current_month ) echo '</ul>';
    
                $current_month = mysql2date('F Y', $result->post_date );
                echo '<h2>'.$current_month.'</h2>';
                echo '<ul>';
            }
            echo '<li>';
            echo ($date_prefix ? mysql2date('M j: ', $result->post_date) : '');
            echo '<a href="'.get_permalink($result->ID).'">';
            echo $result->post_title.'</a></li>';
        }
        if( $current_month ) echo '</ul>';
        if( $display ) {
            ob_end_flush();
        } 
        else {
            return ob_get_clean();
        }
    }
    

    【讨论】:

    • 您能解释一下这与@scompt.com 的回答有何不同吗?
    【解决方案2】:

    我使用了上面的函数,但是将 SQL 查询替换为:

    $results = query_posts('post_type=post&post_status=publish&cat=3');
    

    这让我可以使用@scompt.com 编写的出色功能,但仅限于单个博客类别。

    【讨论】:

      【解决方案3】:

      这是我不久前创建的一个函数。它基本上做你想做的事,但它不是模板。也许你可以适应它。

      <?php
      /**
       * Displays a condensed list of the posts grouped by month/year. 
       *
       * @param $order The order of the posts.  Either 'DESC' or 'ASC', case sensitive.
       * @param $date_prefix Whether to prefix the posts with the month/date.
       * @param $display Whether to display the results or return it as a String. 
       */
      
      function condensed_post_list($order='DESC', $date_prefix=true, $display=true){
          global $wpdb;
      
          if( !in_array($order, array('DESC','ASC' ) ) ) $order = 'DESC';
          $query = "SELECT ID, post_title, post_date FROM $wpdb->posts ".
                   "WHERE post_type='post' AND post_status = 'publish' ".
                   "ORDER BY post_date $order";
          $results = $wpdb->get_results( $query );
      
          ob_start();
          $current_month = '';
          foreach( $results as $result ) {
              if( $current_month != mysql2date('F Y', $result->post_date)) {
                  if( $current_month ) echo '</ul>';
      
                  $current_month = mysql2date('F Y', $result->post_date );
                  echo '<h2>'.$current_month.'</h2>';
                  echo '<ul>';
              }
              echo '<li>';
              echo ($date_prefix ? mysql2date('M j: ', $result->post_date) : '');
              echo '<a href="'.get_permalink($result->ID).'">';
              echo $result->post_title.'</a></li>';
          }
          if( $current_month ) echo '</ul>';
      
          if( $display ) {
              ob_end_flush();
          } else {
              return ob_get_clean();
          }
      }
      ?>
      

      【讨论】:

      • 谢谢你,你的 sn-p 帮了我很多,我设法创建了存档页面。唯一的区别是我没有直接查询数据库,而是使用 Wordpress 的 get_posts 函数。结果如下:victorhurdugaci.com/archive
      • 谢谢!这是一个很大的帮助——尽管我使用了一些原生 wordpress 函数而不是你的 SQL 查询——因为我只需要从一个类别中提取帖子。我用 - $results = query_posts('post_type=post&post_status=publish&cat=3'); 替换了您的 SQL不过帮助很大!谢谢!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-18
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多