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>
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
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>';
}
?>