Widgets: Pre-select currently viewed archive in the Archives dropdown widget, for consistency with the Categories widget.
Props robobot3000, audrasjb, afercia, Themonic. Fixes #40662. git-svn-id: https://develop.svn.wordpress.org/trunk@44897 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
45faac902d
commit
9265ff8a2f
|
@ -1660,21 +1660,23 @@ function get_the_post_type_description() {
|
|||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @param string $url URL to archive.
|
||||
* @param string $text Archive text description.
|
||||
* @param string $format Optional, default is 'html'. Can be 'link', 'option', 'html', or custom.
|
||||
* @param string $before Optional. Content to prepend to the description. Default empty.
|
||||
* @param string $after Optional. Content to append to the description. Default empty.
|
||||
* @param string $url URL to archive.
|
||||
* @param string $text Archive text description.
|
||||
* @param string $format Optional, default is 'html'. Can be 'link', 'option', 'html', or custom.
|
||||
* @param string $before Optional. Content to prepend to the description. Default empty.
|
||||
* @param string $after Optional. Content to append to the description. Default empty.
|
||||
* @param bool $selected Optional. Set to true if the current page is the selected archive page.
|
||||
* @return string HTML link content for archive.
|
||||
*/
|
||||
function get_archives_link( $url, $text, $format = 'html', $before = '', $after = '' ) {
|
||||
function get_archives_link( $url, $text, $format = 'html', $before = '', $after = '', $selected = false ) {
|
||||
$text = wptexturize( $text );
|
||||
$url = esc_url( $url );
|
||||
|
||||
if ( 'link' == $format ) {
|
||||
$link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n";
|
||||
} elseif ( 'option' == $format ) {
|
||||
$link_html = "\t<option value='$url'>$before $text $after</option>\n";
|
||||
$selected_attr = $selected ? " selected='selected'" : '';
|
||||
$link_html = "\t<option value='$url'$selected_attr>$before $text $after</option>\n";
|
||||
} elseif ( 'html' == $format ) {
|
||||
$link_html = "\t<li>$before<a href='$url'>$text</a>$after</li>\n";
|
||||
} else { // custom
|
||||
|
@ -1686,6 +1688,7 @@ function get_archives_link( $url, $text, $format = 'html', $before = '', $after
|
|||
*
|
||||
* @since 2.6.0
|
||||
* @since 4.5.0 Added the `$url`, `$text`, `$format`, `$before`, and `$after` parameters.
|
||||
* @since 5.2.0 Added the `$selected` parameter.
|
||||
*
|
||||
* @param string $link_html The archive HTML link content.
|
||||
* @param string $url URL to archive.
|
||||
|
@ -1693,15 +1696,17 @@ function get_archives_link( $url, $text, $format = 'html', $before = '', $after
|
|||
* @param string $format Link format. Can be 'link', 'option', 'html', or custom.
|
||||
* @param string $before Content to prepend to the description.
|
||||
* @param string $after Content to append to the description.
|
||||
* @param bool $selected True if the current page is the selected archive.
|
||||
*/
|
||||
return apply_filters( 'get_archives_link', $link_html, $url, $text, $format, $before, $after );
|
||||
return apply_filters( 'get_archives_link', $link_html, $url, $text, $format, $before, $after, $selected );
|
||||
}
|
||||
|
||||
/**
|
||||
* Display archive links based on type and format.
|
||||
*
|
||||
* @since 1.2.0
|
||||
* @since 4.4.0 $post_type arg was added.
|
||||
* @since 4.4.0 The `$post_type` argument was added.
|
||||
* @since 5.2.0 The `$year`, `$monthnum`, `$day`, and `$w` arguments were added.
|
||||
*
|
||||
* @see get_archives_link()
|
||||
*
|
||||
|
@ -1729,6 +1734,10 @@ function get_archives_link( $url, $text, $format = 'html', $before = '', $after
|
|||
* @type string $order Whether to use ascending or descending order. Accepts 'ASC', or 'DESC'.
|
||||
* Default 'DESC'.
|
||||
* @type string $post_type Post type. Default 'post'.
|
||||
* @type string $year Year. Default current year.
|
||||
* @type string $monthnum Month number. Default current month number.
|
||||
* @type string $day Day. Default current day.
|
||||
* @type string $w Week. Default current week.
|
||||
* }
|
||||
* @return string|void String when retrieving.
|
||||
*/
|
||||
|
@ -1745,6 +1754,10 @@ function wp_get_archives( $args = '' ) {
|
|||
'echo' => 1,
|
||||
'order' => 'DESC',
|
||||
'post_type' => 'post',
|
||||
'year' => get_query_var( 'year' ),
|
||||
'monthnum' => get_query_var( 'monthnum' ),
|
||||
'day' => get_query_var( 'day' ),
|
||||
'w' => get_query_var( 'w' ),
|
||||
);
|
||||
|
||||
$r = wp_parse_args( $args, $defaults );
|
||||
|
@ -1820,7 +1833,8 @@ function wp_get_archives( $args = '' ) {
|
|||
if ( $r['show_post_count'] ) {
|
||||
$r['after'] = ' (' . $result->posts . ')' . $after;
|
||||
}
|
||||
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
|
||||
$selected = is_archive() && (string) $r['year'] === $result->year && (string) $r['monthnum'] === $result->month;
|
||||
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
|
||||
}
|
||||
}
|
||||
} elseif ( 'yearly' == $r['type'] ) {
|
||||
|
@ -1842,7 +1856,8 @@ function wp_get_archives( $args = '' ) {
|
|||
if ( $r['show_post_count'] ) {
|
||||
$r['after'] = ' (' . $result->posts . ')' . $after;
|
||||
}
|
||||
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
|
||||
$selected = is_archive() && (string) $r['year'] === $result->year;
|
||||
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
|
||||
}
|
||||
}
|
||||
} elseif ( 'daily' == $r['type'] ) {
|
||||
|
@ -1865,7 +1880,8 @@ function wp_get_archives( $args = '' ) {
|
|||
if ( $r['show_post_count'] ) {
|
||||
$r['after'] = ' (' . $result->posts . ')' . $after;
|
||||
}
|
||||
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
|
||||
$selected = is_archive() && (string) $r['year'] === $result->year && (string) $r['monthnum'] === $result->month && (string) $r['day'] === $result->dayofmonth;
|
||||
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
|
||||
}
|
||||
}
|
||||
} elseif ( 'weekly' == $r['type'] ) {
|
||||
|
@ -1901,7 +1917,8 @@ function wp_get_archives( $args = '' ) {
|
|||
if ( $r['show_post_count'] ) {
|
||||
$r['after'] = ' (' . $result->posts . ')' . $after;
|
||||
}
|
||||
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
|
||||
$selected = is_archive() && (string) $r['year'] === $result->yr && (string) $r['w'] === $result->week;
|
||||
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1924,7 +1941,8 @@ function wp_get_archives( $args = '' ) {
|
|||
} else {
|
||||
$text = $result->ID;
|
||||
}
|
||||
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'] );
|
||||
$selected = $result->ID === get_the_ID();
|
||||
$output .= get_archives_link( $url, $text, $r['format'], $r['before'], $r['after'], $selected );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ class WP_Widget_Archives extends WP_Widget {
|
|||
$dropdown_id = "{$this->id_base}-dropdown-{$this->number}";
|
||||
?>
|
||||
<label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"><?php echo $title; ?></label>
|
||||
<select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
|
||||
<select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown">
|
||||
<?php
|
||||
/**
|
||||
* Filters the arguments for the Archives widget drop-down.
|
||||
|
@ -104,6 +104,21 @@ class WP_Widget_Archives extends WP_Widget {
|
|||
<?php wp_get_archives( $dropdown_args ); ?>
|
||||
|
||||
</select>
|
||||
|
||||
<script type='text/javascript'>
|
||||
/* <![CDATA[ */
|
||||
(function() {
|
||||
var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
|
||||
function onSelectChange() {
|
||||
if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) {
|
||||
document.location.href = this.options[ this.selectedIndex ].value;
|
||||
}
|
||||
}
|
||||
dropdown.onchange = onSelectChange;
|
||||
})();
|
||||
/* ]]> */
|
||||
</script>
|
||||
|
||||
<?php } else { ?>
|
||||
<ul>
|
||||
<?php
|
||||
|
|
Loading…
Reference in New Issue