代码之家  ›  专栏  ›  技术社区  ›  Victor Hurdugaci

Wordpress中的存档模板

  •  1
  • Victor Hurdugaci  · 技术社区  · 17 年前

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

    2009年8月

    • 邮政4
    • 职位3
    • 职位1

    所以,基本上,我想要博客上的所有帖子,按日期降序,按月分组。

    谢谢

    附言:Wordpress版本是2.8.2

    3 回复  |  直到 17 年前
        1
  •  3
  •   Edward Dale    17 年前

    <?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();
        }
    }
    ?>
    
        2
  •  0
  •   AshBrad    14 年前

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

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

    这使我能够使用@scompt.com编写的优秀功能,但仅限于一个博客类别。

        3
  •  0
  •   David Rodriguez    14 年前

    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();
        }
    }
    
    推荐文章