Forums

Sort by TAG dropdown list for archives.php file. HELP PLEASE! (3 posts)

  1. pbul2004
    Member
    Posted 5 months ago #

    Hi,

    I want to be able to display a drop down list of tags on the archives.php file to enable a sort/filter on a list of posts.

    For example, i have 20 posts, each 5 posts have a tag of "Cheshire", 10 have a tag of "London" and the other 5 have a tag of "Kent".

    The tag = an Area.

    I want to be able to select tags (areas) that are only active on the posts in a drop down list.

    So there are only Cheshire, London and Kent. I dont want to display the other 30+ different areas. Only the areas / tags that are asigned to the posts listed.

    Then when they select one of the tags such as Kent, it will then display posts only for Kent?

    So what im after is a sort function in a dropdown for active tags on the post lists page?

    Hope you can help,

    Thanks
    Paul

  2. bloomcreative
    Member
    Posted 1 week ago #

    There is a function in the codex for get_the_tags that creates a dropdown list of tags for sorting: http://codex.wordpress.org/Function_Reference/get_the_tags#Function_to_show_tags_in_a_dropdown

    I can't get it to work properly in archives.php (just returns the last tag on the page). get_the_tags needs to be in the loop to work properly but if you put this function in the loop, you get 10+ dropdowns and that's not what we're after obviously. I modified the function to this and it still doesn't work:

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    Any help out there?

  3. bloomcreative
    Member
    Posted 1 week ago #

    Fixed. Use get_tags() instead. Does not need the loop:

    function display_tags() {
    
    	$tags = get_tags(); 
    
    	if ($tags) {
    		echo '<p>Filter by: ';
        	echo "<select onChange=\"document.location.href=this.options[this.selectedIndex].value;\">";
        	echo "<option>Tags</option>\n";
        	foreach ($tags as $tag) {
            	echo "<option value=\"";
            	echo get_tag_link($tag->term_id);
           	 	echo "\">".$tag->name."</option>\n";
        	}
        	echo "</select>";
        	echo '</p>';
        }
    
    }

Reply

You must log in to post.

About this Topic