Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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 :D

share|improve this answer
    
thank You very much :) –  jimilesku Oct 24 '11 at 15:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.