仅包含当年月份的档案

时间:2013-12-31 作者:ruude3

我正在尝试了解如何获取以下存档导航:

2012年1月-2月-3月-4月-5月-6月-7月-8月-9月-10月-11月-12月| 2014年

因此,一个包含当前或所选年份月份的列表,其中包含指向上一年和下一年存档的链接。

我试图编辑Compact Archives 插件,这非常接近我所需要的。它显示所有年份的月份,包括没有帖子的月份。但是,我无法让它仅显示当前/所选年份的月份。我知道这和for each 语句,但我对php的知识不是很好,因此我无法对其进行必要的自定义。

这是我迄今为止定制的:

    function get_compact_archive( $style=\'initial\', $before=\'<li>\', $after=\'</li>\' ) {
global $wpdb, $wp_version;
setlocale(LC_ALL,WPLANG); // set localization language
$below21 = version_compare($wp_version, \'2.1\',\'<\');
// WP 2.1 changed the way post_status and post_type fields work
if ($below21) {
    $now = current_time(\'mysql\');
    $results = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month FROM " . $wpdb->posts . " WHERE post_date <\'" . $now . "\' AND post_status=\'publish\' AND post_password=\'\' ORDER BY year DESC, month DESC");
} else {
    $results = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month FROM " . $wpdb->posts . " WHERE post_type=\'post\' AND post_status=\'publish\' AND post_password=\'\' ORDER BY year DESC, month DESC");
}
if (!$results) {
    return $before.__(\'Archive is empty\').$after;
}
$dates = array();
foreach ($results as $result) {
    $dates[$result->year][$result->month] = 1;
}
unset($results);
$result = \'\';
foreach ($dates as $year => $months){
    $prev_year = $year - 1;
    $next_year = $year + 1;
    $result .= $before . \'<ul class="archive-navigation-months">\';
    for ( $month = 1; $month <= 12; $month += 1) {
        $month_has_posts = (isset($months[$month]));
        $dummydate = strtotime("$month/01/2001");
        // get the month name; strftime() localizes
        $month_name = strftime("%B", $dummydate); 
        switch ($style) {
        case \'initial\':
            $month_abbrev = $month_name[0]; // the inital of the month
            break;
        case \'block\':
            $month_abbrev = strftime("%b", $dummydate); // get the short month name; strftime() localizes
            break;
        case \'numeric\':
            $month_abbrev = strftime("%m", $dummydate); // get the month number, e.g., \'04\'
            break;
        default:
            $month_abbrev = $month_name[0]; // the inital of the month
        }
        if ($month_has_posts) {
            $result .= \'<li><a href="\'.get_month_link($year, $month).\'" title="\'.$month_name.\' \'.$year.\'">\'.$month_abbrev.\'</a></li>\';
        } else {
            $result .= \'<li class="emptymonth">\'.$month_abbrev.\'</li>\';
        }
    }
    $result .= \'</ul><ul class="archive-navigation-years"><li><a href="\'.get_year_link($prev_year).\'">\'.$prev_year.\'</a></li><li><a href="\'.get_year_link($next_year).\'">\'.$next_year.\'</a></li></ul>\' . $after."\\n";
}
return $result;
}

我也检查过了this thread 但这对我也没有帮助。

1 个回复
SO网友:ruude3

我把插件放在一边,最终在我提到的另一个线程中的一个答案的帮助下找到了它。下面的代码满足了我的需要。

function get_posts_grouped_by_month( $year = null ) {

if ( $year == null ) {
    $year = date(\'Y\');
}

$months = range(1,12);
$posts = array();

foreach ( $months as $month ) {
    $posts_for_month = get_posts(array(
        \'year\' => $year,
        \'monthnum\' => $month ));
    $posts[$month] = $posts_for_month;
}

return $posts;
}

function compacter_archives() {
$current_year = get_the_date( \'Y\' );
$previous_year = $current_year - 1;
$next_year = $current_year + 1;
$monthly_posts = get_posts_grouped_by_month($current_year);

if ( function_exists(\'icl_get_languages\') ) {
    global $sitepress;
    $current_lang = $sitepress->get_current_language();
} else {
    $current_lang = get_locale();
}

echo \'<ul class="compacter-archives-month-list">\';
foreach ( $monthly_posts as $month => $posts ) {    
    setlocale(LC_TIME, $current_lang);
    $time = mktime(0, 0, 0, $month);
    $month_name = strftime("%b", $time);

    if ($posts) {
        echo \'<li><a href="\' . get_month_link( $current_year, $month ) . \'">\' . $month_name . \'</a></li>\';
    } else {
        echo \'<li>\' . $month_name . \'</li>\'; 
    }
}
echo \'</ul><ul class="compacter-archives-year-list">\';

$query1 = new WP_Query( array ( \'year\'=> $previous_year ) );
if ( $query1->have_posts() ) {  
    echo \'<li><a href="\' . get_year_link($previous_year) . \'">\' . $previous_year . \'</a></li>\';
} else {
    echo \'<li>\' . $previous_year . \'</li>\';
}
wp_reset_query();

$query2 = new WP_Query( array ( \'year\'=> $next_year ) );
if ( $query2->have_posts() ) {  
    echo \'<li><a href="\' . get_year_link($next_year) . \'">\' . $next_year . \'</a></li>\';
} else {
    echo \'<li>\' . $next_year . \'</li>\';
}
wp_reset_query();
}

结束

相关推荐

private functions in plugins

我开发了两个插件,其中一个功能相同(相同的名称,相同的功能)。当试图激活两个插件时,Wordpress会抛出一个错误,因为它不允许我以相同的名称定义函数两次。有没有一种方法可以使这个函数只对插件私有,而不使用面向对象编程,也不简单地重命名函数?我不想使用OOP,因为我首先要学习它。此外,我不想重命名该函数,因为我可能也想在其他插件中使用它,而重命名感觉不太合适。