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

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

本文介绍了在同一链接上对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进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:08