Архив шаблона в Wordpress - PullRequest
1 голос
/ 03 августа 2009

Я хочу создать шаблон страницы архива для Wordpress, который будет выглядеть так:

август 2009

  • Пост 4
  • Пост 3
  • Пост 2
  • Пост 1

июль 2009

  • Пост 2
  • Пост 1

Итак, я хочу, чтобы все сообщения из блога были отсортированы по дате и сгруппированы по месяцам. Может ли кто-нибудь предоставить мне код PHP для этого?

Спасибо!

PS: версия для Wordpress 2.8.2

Ответы [ 3 ]

3 голосов
/ 04 августа 2009

Это функция, которую я создал некоторое время назад. Он в основном делает то, что вы хотите сделать, но это не шаблон. Может быть, вы можете адаптировать его.

<?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();
    }
}
?>
0 голосов
/ 23 августа 2012

Спасибо большое за помощь. Это то, что я использовал с кодом выше.

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();
    }
}
0 голосов
/ 02 марта 2012

Я использовал вышеуказанную функцию, но заменил SQL-запрос на:

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

Это позволило мне использовать превосходную функцию, написанную @ scompt.com, но ограничить ее одной категорией блога.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...