Brak opisu

archives.php 2.4KB

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