将WordPress存档列表分解为多个列

时间:2012-09-28 21:08:31

标签: wordpress list count archive multiple-columns

我想将此存档列表拆分为多个列(三列)...我不想为此使用CSS3。我可以使用wp_list_categories轻松完成此操作,但该方法不适用于wp_get_archives。

理想情况下,我想实现一个计数器,以大约1/3的数量添加标签......然后我就可以使用CSS了。

CUSTOM ARCHIVE FUNCTION:

function wp_custom_archive($args = '') {
global $wpdb, $wp_locale;
$defaults = array(
    'limit' => '',
    'format' => 'html', 'before' => '',
    'after' => '', 'show_post_count' => false,
    'echo' => 1
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
if ( '' != $limit ) {
    $limit = absint($limit);
    $limit = ' LIMIT '.$limit;
}
// over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
$archive_date_format_over_ride = 0;
// options for daily archive (only if you over-ride the general date format)
$archive_day_date_format = 'Y/m/d';
// options for weekly archive (only if you over-ride the general date format)
$archive_week_start_date_format = 'Y/m/d';
$archive_week_end_date_format   = 'Y/m/d';
if ( !$archive_date_format_over_ride ) {
    $archive_day_date_format = get_option('date_format');
    $archive_week_start_date_format = get_option('date_format');
    $archive_week_end_date_format = get_option('date_format');
}
//filters
$where = apply_filters('customarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r );
$join = apply_filters('customarchives_join', "", $r);
$output = '<ul>';
    $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";
    $key = md5($query);
    $cache = wp_cache_get( 'wp_custom_archive' , 'general');
    if ( !isset( $cache[ $key ] ) ) {
        $arcresults = $wpdb->get_results($query);
        $cache[ $key ] = $arcresults;
        wp_cache_set( 'wp_custom_archive', $cache, 'general' );
    } else {
        $arcresults = $cache[ $key ];
    }
    if ( $arcresults ) {
        $afterafter = $after;
        foreach ( (array) $arcresults as $arcresult ) {
            $url = get_month_link( $arcresult->year, $arcresult->month );
            /* translators: 1: month name, 2: 4-digit year */
            $text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
            $year_text = sprintf('<li>%d</li>', $arcresult->year);
            if ( $show_post_count )
                $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
            $output .= ( $arcresult->year != $temp_year ) ? $year_text : '';
            $output .= get_archives_link($url, $text, $format, $before, $after);
            $temp_year = $arcresult->year;
        }
    }
$output .= '</ul>';
if ( $echo )
    echo $output;
else
    return $output;

}

当前HTML输出(缩短):

<ul>
<li>2012</li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/09/' title='September'>September</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/08/' title='August'>August</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/07/' title='July'>July</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/06/' title='June'>June</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/05/' title='May'>May</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/04/' title='April'>April</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/03/' title='March'>March</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/02/' title='February'>February</a></li>
<li><a href='http://alittlesewing.com.s118336.gridserver.com/2012/01/' title='January'>January</a></li>
</ul>

1 个答案:

答案 0 :(得分:1)

如果我理解正确,你就必须做这样的事情

$output = '<ul class="col1">';
if ( $arcresults ) {
    $numarticles = count($arcresults);
    $column1 = round($numarticles/3);
    $column2 = round($column1*2);
    $counter = 0;
    $afterafter = $after;
    foreach ( (array) $arcresults as $arcresult ) {
        if($counter == $column1) {
           $output .= "</ul><ul class='col2'>";
        } elseif($counter == $column2)  {
           $output .= "</ul><ul class='col3'>";
        }
        $url = get_month_link( $arcresult->year, $arcresult->month );
        /* translators: 1: month name, 2: 4-digit year */
        $text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
        $year_text = sprintf('<li>%d</li>', $arcresult->year);
        if ( $show_post_count )
            $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
        $output .= ( $arcresult->year != $temp_year ) ? $year_text : '';
        $output .= get_archives_link($url, $text, $format, $before, $after);
        $temp_year = $arcresult->year;
        $counter ++;
    }
    $output .= '</ul>';
}

这样,当计数器到达三分之一时,它会添加另一个列表,所以最后你将有3个列表。

希望这有帮助。