WordPress 存档每年都会下降
我在 WordPress 上有一个年度存档下拉选择框。代码如下:
<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>
一旦我选择了年份,比如2006年,下拉框应该显示2006年。目前它显示默认的“选择年份”。
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 should display 2006. At present it shows the default "Select Year".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 wp_get_archives 函数无法设置“默认”选定年份。
因此,您有几种选择:
基于 wp_get_archives 实现您自己的函数。这是一个相当深入的函数,并且可能会在未来的实现中发生变化,因此这可能不是一个好主意。
对从 wp_get_archives 返回的数据进行后处理,以添加当前年份的“selected="selected"”。
对
使用在加载页面时执行的 JavaScript 函数来扫描选择选项并设置默认值。
其中,我可能会选择第二个选项。 (您应该能够根据找到所需的
并将其与
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">
.)