doushi1964 2015-10-09 15:55
浏览 40
已采纳

WordPress - 基于has_tag()的jquery选择器

I have a drop down selection for two choices (eg. books and records). By default is it books. The user will be redirected to another section on the site if they choose records. I want the dropdown to reflect how I have tagged the pages in WordPress using the has_tag condition. For example, if the page is tagged as "records", I want them to see records in the dropdown, and the opposite for books. Following is what I tried:

HTML:

 <select name="segment" id="segment">

  <option value="/book">Book</option>
  <option value="/record">Record</option>
</select>

<script type="text/javascript">
jQuery('#segment').change(function(){
window.location.href = jQuery(this).val();
});

jQuery(function(){
 jQuery("#segment > option").each(function() {
  if(this.value == window.location.pathname){
   this.selected = 'selected';
   }
 });
});

</script>

However, once they are on that page and click to any other page, it defaults to "book" again. I want to say that if the page has_tag('book'), then the option will be book, but if it the page has_tag('record'), then the option will be record. Any suggestions would be appreciated.

  • 写回答

1条回答 默认 最新

  • dos8410 2015-10-09 16:00
    关注

    You can use the selected attribute of the <option> tag to do this. For example:

    <select name="segment" id="segment">
        <option value="/book" <?php if ( has_tag('book') ) { echo 'selected'; } ?>>Book</option>
        <option value="/record" <?php if ( has_tag('record') ) { echo 'selected'; } ?>>Record</option>
    </select>
    

    I would get rid of the .each() function and just use PHP for this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.