php - Wordpress 存档列表中的年份和月份

标签 php wordpress list date archive

我想列出有公开 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>

最佳答案

好的,在 stack exchange 上找到了一个非常有用的帖子:here

基本上,将其添加到 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(); ?>

它将根据需要返回一个列表,按年然后按月,每个列表都有链接!

关于php - Wordpress 存档列表中的年份和月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38985334/

相关文章:

PHP 动态求和回显结果(困难)

php - 在 PHP 更新表单中从 MySQL 记录集中选择多个表单元素

php剥离html标签及其数据并插入到mysql中

html - Visual Composer,页面特定的自定义 css

python - 访问/使用元组列表中的元素 python 3.x

python - 如何清空列表?

php - 如何从 php 中检索值?

php - WP - 检查帖子是否存在于 wp_insert_post 之前

php - 尝试在 wordpress 自定义页面中实现 jQuery 以进行搜索自动完成但不起作用

list - 将列表分成大小为N的组