开发者 | NoseGraze |
---|---|
更新时间 | 2024年3月23日 22:55 |
捐献地址: | 去捐款 |
PHP版本: | 7.4 及以上 |
WordPress版本: | 6.4.3 |
版权: | GPLv2 or later |
版权网址: | 版权信息 |
expanding-archives
to the /wp-content/plugins/
directoryThe plugin does not come with a settings panel so you have to do this with your own custom CSS. Here are a few examples:
Change the year background colour:
.expanding-archives-title { background: #000000; }
Change the year font colour:
.expanding-archives-title a { color: #ffffff; }
By default, the widget includes posts in all categories. You can add the following code to a custom plugin or a child theme's functions.php file to limit the results to posts in a specific category:
`add_filter('expanding_archives_get_posts', function(array $args) {
$args['cat'] = 2; // Replace with ID of your category.
return $args;
});
add_filter('expanding_archives_query', function(string $query) {
$category = get_category(2); // Replace with ID of your category.
if (! $category instanceof \WP_Term) {
return $query;
}
global $wpdb;
return "
SELECT DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year, COUNT(id) as post_count
FROM {$wpdb->posts}
INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id AND {$wpdb->term_relationships}.term_taxonomy_id = 2)
WHERE post_status = 'publish'
AND post_date <= now()
AND post_type = 'post'
GROUP BY month, year
ORDER BY post_date DESC
";
});Be sure to set the ID of your category in both of the designated places (the examples use ID
2`).
Note that the results may not update instantly, as the query to retrieve the date periods is cached for one day. To force the query to re-run, delete this transient: expanding_archives_months
By default, the widget will show a list of every year you've published posts.
If you have a lot of posts, you may wish to only show results from the last few years. This can be done with the following code snippet:
add_filter('expanding-archives/earliest-date', fn() => '-4 years');
You can change -4 years
to any value supported by the PHP DateTime constructor. This can be a relative value (as shown above), or a specific date like:
add_filter('expanding-archives/earliest-date', fn() => '2023-01-01');
Note that the results may not update instantly, as the query to retrieve the date periods is cached for one day. To force the query to re-run, delete this transient: expanding_archives_months
xhrFields: { withCredentials: true }
to ajax call.