与Orderby分开的ASC和DESC

时间:2012-03-25 作者:Andrew

所以我用这个来整理我的帖子,效果很好:

<div class="sort">
   Sort by  
     <select onclick="if (this.value) { document.location = this.value; }">  
        <option value="" selected="selected">Latest first</option>  
        <option value="<?php echo $short_page_url; ?>?orderby=date">Oldest first</option>  
        <option value="<?php echo $short_page_url; ?>?orderby=title">Titles A-Z</option>  
     </select>
</div>
但是,如何将顺序作为单独的下拉列表/链接在ASC和DESC之间进行更改?因此,当存在“?orderby=title/date”时,它会将“&;order=desc/asc”添加到链接中?

1 个回复
最合适的回答,由SO网友:xaedes 整理而成

我会这样做:

<script type="text/javascript">
    //http://www.netlobo.com/url_query_string_javascript.html
    function gup( name, default_value )
    {
      name = name.replace(/[\\[]/,"\\\\\\[").replace(/[\\]]/,"\\\\\\]");
      var regexS = "[\\\\?&]"+name+"=([^&#]*)";
      var regex = new RegExp( regexS );
      var results = regex.exec( window.location.href );
      if( results == null )
        return default_value;
      else
        return results[1];
    }   
    //here you can output default values for sortby and order by your php script
    var orderby = gup("orderby","");
    var order = gup("order","");
    function change_url() {
        var url = "<?php echo $short_page_url; ?>?orderby="+orderby+"&order="+order;
        document.location = url;
    }

    //you still have to let javascript or php select the correct options
</script>
<div class="sort">
   Sort by  
     <select onclick="if (this.value) { orderby = this.value; change_url(); }">  
        <option value="" selected="selected">Latest first</option>  
        <option value="date">Oldest first</option>  
        <option value="title">Titles A-Z</option>  
     </select>
   Order 
     <select onclick="if (this.value) { order = this.value; change_url(); }">  
        <option value="asc">asc</option>  
        <option value="desc">desc</option>  
     </select>
</div>

结束

相关推荐

帖子2帖子:查询已连接--orderby问题

使用Posts 2 Posts plugin 我已经创建了\'piece\', \'person\' 和\'collection\' 自定义帖子类型:任何文章都可以连接到个人(具有角色\'author\', \'conductor\' 等)和收集。现在,相关代码如下所示:// get all audio pieces connected to current $person: $audio_ascond = p2p_type( \'pieces_persons\' )->get_connec