Wordpress归档列表中的年份和月份

5
我想列出所有有公共WordPress文章的年份,并在下面的子菜单中列出这些年份的月份。例如:
  • 2016
    • 八月
    • 三月
  • 2015
    • 六月
    • 五月
    • 二月
我已经在循环中编写了一个工作版本,但需要手动添加新列表来完成每一年。是否有任何方法可以自动调整它,以便自动包括新年?
<h1>2016</h1>
<ul>
     <?php
$string = wp_get_archives('type=monthly&year=2016&echo=0');
$pattern = ' ((19|20)\d{2}(</a>))';
echo preg_replace($pattern, '\\3', $string); '<li></li>';?>
</ul></a>
<h1>2015</h1>
 <?php
$string = wp_get_archives('type=monthly&year=2015&echo=0');
$pattern = ' ((19|20)\d{2}(</a>))';
echo preg_replace($pattern, '\\3', $string); '<li></li>';?>
</ul></a>
4个回答

5

好的,我在stack exchange上找到了一篇非常有用的帖子:这里

基本上,在functions.php中添加以下内容:

//List archives by year, then month
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 );
                $year_url = get_year_link($arcresult->year);
                /* translators: 1: month name, 2: 4-digit year */
                $text = sprintf(__('%s'), $wp_locale->get_month($arcresult->month));
                $year_text = sprintf('%d', $arcresult->year);
                if ( $show_post_count )
                    $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
                $year_output = get_archives_link($year_url, $year_text, $format, $before, $after);              
                $output .= ( $arcresult->year != $temp_year ) ? $year_output : '';
                $output .= get_archives_link($url, $text, $format, $before, $after);

                $temp_year = $arcresult->year;
            }
        }

    $output .= '</ul>';

    if ( $echo )
        echo $output;
    else
        return $output;
}

然后在页面上调用此函数:

<?php wp_custom_archive(); ?>

它将按年份和月份返回一个列表,并提供每个链接,完全符合需求!

你知道如何在每个月份下输出文章标题、内容等吗? - la_petite_kozel

1

我会用下面这种方式实现。

这种方式比较简洁易懂,而且使用了WordPress核心函数处理数据,通常是处理WP数据的首选方式。此外,它不需要昂贵的正则表达式功能。

1. 将“in_year”选项添加到wp_get_archives()函数中

<?php
    add_filter( 'getarchives_where', 'archives_in_specific_year', 10, 2 );
    function archives_in_specific_year( $sql_where, $parsed_args ) {
        if ( ! empty( $parsed_args['in_year'] ) ) {
            $year       = absint( $parsed_args['in_year'] );
            $sql_where .= " AND YEAR(post_date) = $year";
        }
        return $sql_where;
    }
?>

2. 每年,呈现该年及其月份

<?php
    $args = [
        'type' => 'yearly',
        'echo' => false,
    ];

    $years       = wp_get_archives( $args );
    $years_array = explode( "\t", trim( wp_strip_all_tags( $years ) ) );

    foreach ( $years_array as $current_year ) {
        $current_year = intval( $current_year );

        $monthly_args = [
            'type'    => 'monthly',
            'format'  => 'custom',
            'echo'    => false,
            'before'  => '<span>',
            'after'   => '</span>',
            'in_year' => $current_year,
        ];

        $months = str_replace( ' ' . $current_year, '', wp_get_archives( $monthly_args ) );

        $yearly_args = [
            'type'    => 'yearly',
            'after'   => '<div>' . $months . '</div>',
            'in_year' => $current_year,
        ];

        wp_get_archives( $yearly_args );
    }
?>

1
 <select name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
  <option value=""><?php echo esc_attr( __( 'Select Month' ) ); ?></option> 
  <?php wp_get_archives( array( 'type' => 'monthly', 'format' => 'option', 'show_post_count' => 1 ) ); ?>
</select>

0

这里有一个简单的MySQL查询,解决了一个类似的问题。

$query = 'SELECT DISTINCT MONTH(`post_date`) AS `month`, YEAR(`post_date`) AS `year` FROM `wp_posts` ORDER BY `post_date` ASC';
$results = $wpdb->get_results($query);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接