I have a form in WordPress with options when clicked they retrieve a filtered posts. I want to take the options from the form and place it like normal links. Can this be done? Thank You all in advance 🙂
<form action="" method="get" id="filter">
<select id="sort_by" name="sort_by" onchange="this.form.submit()">
<option value="most_recent"<?php if( !$_GET['sort_by'] || $_GET['sort_by'] == 'most_recent' ) echo " selected"; ?>><?php print
__('Most Recent', 'bo'); ?></option>
<option value="most_favorites"<?php if( $_GET['sort_by'] == 'most_favorites' ) echo " selected"; ?>><?php print __('Most Favorites', 'bo'); ?></option>
<option value="most_viewed"<?php if( $_GET['sort_by'] == 'most_viewed' ) echo " selected"; ?>><?php print __('Most Viewed', 'bo'); ?></option>
<option value="most_commented"<?php if( $_GET['sort_by'] == 'most_commented' ) echo " selected"; ?>><?php print __('Most Commented', 'bo'); ?></option>
</select>
</form>
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: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.Finally we construct the links. Here I’ve used
$active
to conditionally add the ‘active’ class when that sort method is currently being used.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 😀