Yes. First of all, to be able to highlight the current sorting method, we attempt to retrieve if the GET variable sort_by
occurs in the url:
<?php switch ($_GET["sort_by"]) {
case 'most_recent':
$active="most_recent";
break;
case 'most_favourite':
$active="most_favourite";
break;
case 'most_viewed':
$active="most_viewed";
break;
default:
// However you sort by default
$active="most_recent";
}?>
If a valid sort order is present, $active
is set to this. Otherwise it is set to the default order. We shall use this later.
Next we retrieve the url, then remove the sort_by
GET variable. This allows us to manually add it later to the links without having duplicates.
<?php
//Retrieve _GET variables
$params = $_GET;
//Retrieve 'sort_by' variable
unset($params['sort_by']);;
//Rebuild url without 'sort_by' variable
$url = get_pagenum_link();
$url = strtok($url, '?');
$url =$url.'?'.http_build_query($params);
?>
Finally we construct the links. Here I’ve used $active
to conditionally add the ‘active’ class when that sort method is currently being used.
<ul>
<li <?php if($active == "most_recent"): ?> class="active"<? endif ?>><a href="http://<?php echo $url ?>&sort_by=most_recent">Most recent </a></li>
<li <?php if($active == "most_favourite"): ?> class="active"<? endif ?>><a href="http://<?php echo $url ?>&sort_by=most_favourite">Most favourite</a></li>
<li <?php if($active == "most_viewed"): ?> class="active"<? endif ?>><a href="http://<?php echo $url ?>&sort_by=most_viewed">Most viewed </a></li>
</ul>
All the above code should appear wherever you want the links to occur. You can put it inside your functions.php
, inside a function, however, and then call that function wherever you want the links to appear.
Hope this helps 😀