恐怕,修改原始widget的输出会非常困难。它使用wp_get_archives
函数打印存档文件,没有简单的方法可以修改此输出。你可以尝试使用get_archives_link
, 但它可能会变得有点混乱。
说。。。还有其他更简单的方法——编写自己的小部件。
class WP_Widget_ArchivesByYear extends WP_Widget {
public function __construct() {
$widget_ops = array(
\'classname\' => \'widget_archive_by_year\',
\'description\' => __( \'A monthly archive of your site’s Posts displayed by year.\' ),
\'customize_selective_refresh\' => true,
);
parent::__construct(\'archives_by_year\', __(\'Archives by Year\'), $widget_ops);
}
public function widget( $args, $instance ) {
global $wpdb;
$title = apply_filters( \'widget_title\', empty( $instance[\'title\'] ) ? __( \'Archives\' ) : $instance[\'title\'], $instance, $this->id_base );
echo $args[\'before_widget\'];
if ( $title ) {
echo $args[\'before_title\'] . $title . $args[\'after_title\'];
}
$years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM {$wpdb->posts} WHERE post_type = \'post\' AND post_status = \'publish\' ORDER BY post_date DESC");
if ( $years ) :
?>
<ul class="years-list">
<?php
foreach ( $years as $year ) :
$months = $wpdb->get_col( $wpdb->prepare("SELECT DISTINCT MONTH(post_date) FROM {$wpdb->posts} WHERE post_type=\'post\' AND post_status=\'publish\' AND YEAR(post_date) = %d ORDER BY post_date ASC", $year));
?>
<li class="year">
<a href="<?php echo get_year_link($year); ?>"><?php echo $year ?></a>
<ul class="months-list">
<?php
foreach ( $months as $month ) :
$dateObj = DateTime::createFromFormat(\'!m\', $month);
?>
<li class="month">
<a href="<?php echo get_month_link($year, $month); ?>"><?php echo $dateObj->format(\'F\'); ?></a>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
<?php
endif;
echo $args[\'after_widget\'];
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$new_instance = wp_parse_args( (array) $new_instance, array( \'title\' => \'\') );
$instance[\'title\'] = sanitize_text_field( $new_instance[\'title\'] );
return $instance;
}
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( \'title\' => \'\') );
$title = sanitize_text_field( $instance[\'title\'] );
?>
<p><label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Title:\'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
<?php
}
}
另外,我还没有测试那个代码,所以它可能包含一些拼写错误。