archives.php 2.0 KB

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