Wordpress Archive drop down yearly
I have a yearly archive drop-down select box on wordpress. Here is the code:
<select name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( 'Select Year' ) ); ?></option>
<?php wp_get_archives( 'type=yearly&format=option&show_post_count=0' ); ?>
</select>
Once I select a year, say 2006, the drop down box shoul开发者_如何学编程d display 2006. At present it shows the default "Select Year".
The problem is that the wp_get_archives function has no ability to set a "default" selected year.
As such you have a few alternatives:
Implement your own function based on wp_get_archives. It's quite a deep function and may change in future implementations, so this probably isn't a good idea.
Post process the data returned from wp_get_archives to add in a ' selected="selected"' for the current year.
Use a JavaScript function that executes when the page is loaded to scan the select options and set the default.
Of these, I'd probably go with the second option. (You should simply be able to do a str_replace based on finding the required <option value='XXX'>
and swapping it with <option value='XXX' selected="selected">
.)
精彩评论