Customize widget archives

This commit is contained in:
Roman Pyrih
2024-09-27 14:07:49 +02:00
parent 7175b922e1
commit 25ec95e67b
8 changed files with 130 additions and 17 deletions

View File

@@ -135,20 +135,9 @@ class WP_Widget_Archives extends WP_Widget {
}
?>
<ul>
<!-- <ul>
<?php
wp_get_archives(
/**
* Filters the arguments for the Archives widget.
*
* @since 2.8.0
* @since 4.9.0 Added the `$instance` parameter.
*
* @see wp_get_archives()
*
* @param array $args An array of Archives option arguments.
* @param array $instance Array of settings for the current widget.
*/
apply_filters(
'widget_archives_args',
array(
@@ -159,6 +148,51 @@ class WP_Widget_Archives extends WP_Widget {
)
);
?>
</ul> -->
<ul class="archive-list">
<?php
global $wpdb;
$years = $wpdb->get_results( "
SELECT DISTINCT YEAR(post_date) AS year, COUNT(ID) as post_count
FROM $wpdb->posts
WHERE post_type = 'post' AND post_status = 'publish'
GROUP BY year
ORDER BY post_date DESC
" );
foreach ( $years as $year ) {
?>
<li class="archive-year">
<a href="javascript:void(0);" class="archive-year-toggle">
<?php echo esc_html( $year->year ); ?> (<?php echo esc_html( $year->post_count ); ?>)
</a>
<ul class="archive-months" style="display:none;">
<?php
$months = $wpdb->get_results( $wpdb->prepare("
SELECT DISTINCT MONTH(post_date) AS month, COUNT(ID) as post_count
FROM $wpdb->posts
WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = %d
GROUP BY month
ORDER BY post_date DESC
", $year->year) );
foreach ( $months as $month ) {
$month_name = date_i18n( 'F', mktime( 0, 0, 0, $month->month, 1 ) );
?>
<li class="archive-month">
<a href="<?php echo get_month_link( $year->year, $month->month ); ?>">
<?php echo esc_html( $month_name . ' (' . $month->post_count . ')' ); ?>
</a>
</li>
<?php
}
?>
</ul>
</li>
<?php
}
?>
</ul>
<?php