Archives List Shortcode
Adds an [archives] shortcode that lists your posts by month or year, as a list or a dropdown, anywhere in content or widgets.
Opens WPIDE on
Code
add_shortcode( 'archives', function ( $atts ) {
$atts = shortcode_atts( array(
'type' => 'monthly',
'limit' => '',
'show_count' => 'no',
'dropdown' => 'no',
'post_type' => 'post',
), $atts, 'archives' );
$args = array(
'type' => in_array( $atts['type'], array( 'yearly', 'monthly', 'weekly', 'daily', 'postbypost', 'alpha' ), true ) ? $atts['type'] : 'monthly',
'limit' => $atts['limit'] ? absint( $atts['limit'] ) : '',
'show_post_count' => 'yes' === $atts['show_count'],
'post_type' => post_type_exists( $atts['post_type'] ) ? $atts['post_type'] : 'post',
'echo' => false,
);
if ( 'yes' === $atts['dropdown'] ) {
$args['format'] = 'option';
return '<select class="wpide-archives-dropdown" aria-label="Archives" onchange="if(this.value){window.location.href=this.value;}"><option value="">Select a period</option>' . wp_get_archives( $args ) . '</select>';
}
return '<ul class="wpide-archives">' . wp_get_archives( $args ) . '</ul>';
} );
Notes
Example: [archives type="yearly" show_count="yes"] or [archives type="monthly" limit="12" dropdown="yes"].
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.