开发者

Wordpress Archive drop down yearly

开发者 https://www.devze.com 2023-02-03 21:06 出处:网络
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;\">

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:

  1. 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.

  2. Post process the data returned from wp_get_archives to add in a ' selected="selected"' for the current year.

  3. 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">.)

0

精彩评论

暂无评论...
验证码 换一张
取 消