I'm afraid, it would be pretty hard to modify the output of original widget. It uses wp_get_archives
function to print the archive and there is no easy way to modify this output. You could try to use get_archives_link
, but it can get a little bit messy.
Saying that... There is other, much simpler way - writing your own widget.
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
}
}
PS. I haven't tested that code, so it can contain some typos.
<ul>
, making it impossible to split the archive into two loops. Also there is no option to filter the output ofwp_get_archives
. What you want can certainly be done, but not with the default archives widget.