archives.php
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * Archives shortcode |
| 5 | * @author bubel & nickmomrik |
| 6 | * [archives limit=10] |
| 7 | */ |
| 8 | |
| 9 | add_shortcode( 'archives', 'archives_shortcode' ); |
| 10 | |
| 11 | function archives_shortcode( $atts ) { |
| 12 | if ( is_feed() ) { |
| 13 | return '[archives]'; |
| 14 | } |
| 15 | |
| 16 | global $allowedposttags; |
| 17 | |
| 18 | $default_atts = array( |
| 19 | 'type' => 'postbypost', |
| 20 | 'limit' => '', |
| 21 | 'format' => 'html', |
| 22 | 'showcount' => false, |
| 23 | 'before' => '', |
| 24 | 'after' => '', |
| 25 | 'order' => 'desc', |
| 26 | ); |
| 27 | |
| 28 | $attr = shortcode_atts( $default_atts, $atts, 'archives' ); |
| 29 | |
| 30 | if ( ! in_array( $attr['type'], array( 'yearly', 'monthly', 'daily', 'weekly', 'postbypost' ) ) ) { |
| 31 | $attr['type'] = 'postbypost'; |
| 32 | } |
| 33 | |
| 34 | if ( ! in_array( $attr['format'], array( 'html', 'option', 'custom' ) ) ) { |
| 35 | $attr['format'] = 'html'; |
| 36 | } |
| 37 | |
| 38 | $limit = intval( $attr['limit'] ); |
| 39 | // A Limit of 0 makes no sense so revert back to the default. |
| 40 | if ( empty( $limit ) ) { |
| 41 | $limit = ''; |
| 42 | } |
| 43 | |
| 44 | $showcount = ( false !== $attr['showcount'] && 'false' !== $attr['showcount'] ) ? true : false; |
| 45 | $before = wp_kses( $attr['before'], $allowedposttags ); |
| 46 | $after = wp_kses( $attr['after'], $allowedposttags ); |
| 47 | |
| 48 | // Get the archives |
| 49 | $archives = wp_get_archives( array( |
| 50 | 'type' => $attr['type'], |
| 51 | 'limit' => $limit, |
| 52 | 'format' => $attr['format'], |
| 53 | 'echo' => false, |
| 54 | 'show_post_count' => $showcount, |
| 55 | 'before' => $before, |
| 56 | 'after' => $after, |
| 57 | ) ); |
| 58 | |
| 59 | if ( 'asc' === $attr['order'] ) { |
| 60 | $archives = implode( "\n", array_reverse( explode( "\n", $archives ) ) ); |
| 61 | } |
| 62 | |
| 63 | // Check to see if there are any archives |
| 64 | if ( empty( $archives ) ) { |
| 65 | $archives = '<p>' . __( 'Your blog does not currently have any published posts.', 'jetpack' ) . '</p>'; |
| 66 | } else if ( 'option' === $attr['format'] ) { |
| 67 | $archives = '<select name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;"><option value="' . get_permalink() . '">--</option>' . $archives . '</select>'; |
| 68 | } else if ( 'html' === $attr['format'] ) { |
| 69 | $archives = '<ul>' . $archives . '</ul>'; |
| 70 | } |
| 71 | |
| 72 | return $archives; |
| 73 | } |
| 74 |