在同一链接上对ASC和DESC进行排序

编程入门 行业动态 更新时间:2024-10-22 11:34:31
本文介绍了在同一链接上对ASC和DESC进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目前,我正在使用WordPress,并有一个列表,可以在其中订购asc和desc帖子,但带有单个链接,例如"A-Z"和"Z-A".

Currently I am working on WordPress and have a list where I can order posts asc and desc but with individual links such as "A-Z" and "Z-A".

我想要实现的是能够使用户单击与单击列表时按升序和降序排序的列表相同的链接.

What I would like to achieve is to be able to have the user click the same link as have the list be sorted asc and desc on click.

以下是我目前拥有的代码,但我不确定如何将其组合到一个链接中.

The following is my code for what I currently have but I am not sure how to approach this to combine in one link.

<?php $type = 'test'; $args=array( 'post_type' => $type, 'post_status' => 'publish', ); if( isset( $_GET['sort'] ) && "titleasc" == $_GET['sort'] ){ $args['orderby'] = 'title'; $args['order'] = 'ASC'; } if( isset( $_GET['sort'] ) && "titledesc" == $_GET['sort'] ){ $args['orderby'] = 'title'; $args['order'] = 'DESC'; }

我的示例链接是标准链接:

The example link I have is the standard:

<th>Title <span><a href="?sort=titleasc">A-Z</a> <a href="?sort=titledesc">Z-A</a></span> </th>

我想我可以写一个elseif,但是当我尝试写它时,它要么中断要么根本不变.我认为想法是代码需要检查它是asc还是desc,然后执行相反的操作.我只是不太确定该如何用我有限的知识来编写它.

I am thinking I can write an elseif but when I attempt to it either breaks or doesn't change at all. I think the idea is that the code needs to check whether it is asc or desc already and then do the opposite of that. I am just not entirely sure how to write it with my limited knowledge.

给出的任何方向都将非常有帮助!谢谢

Any direction given will be super helpful! Thanks

推荐答案

所有您需要做的就是在HTML中添加您在php中使用的相同逻辑(当然还有php ;-))

All you have to do is add the same logic you are using in your php within your HTML (with php of course ;-) )

<?php if( isset( $_GET['sort'] ) && "titleasc" == $_GET['sort'] ){ echo '<a href="?sort=titledesc">Z-A</a>'; } if( isset( $_GET['sort'] ) && "titledesc" == $_GET['sort'] ){ echo '<a href="?sort=titleasc">A-Z</a>'; } ?>

更多推荐

在同一链接上对ASC和DESC进行排序

本文发布于:2023-10-23 05:54:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519985.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   在同一   DESC   ASC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!