WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Drop Down List for Tags (11 posts)

  1. noelgreen
    Member
    Posted 6 years ago #

    Is there any way to make a dropdown list for Tags?
    Not for posting, but on the published blog... so that people can choose to view entries by tag using a dropdown.

    If there is an easy way, I'm missing it.
    Any help is appreciated.

  2. noelgreen
    Member
    Posted 6 years ago #

    Okay, I figured out a drop down... sort of.

    This makes the dropdown:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    Category: <select name='tag' id='tag' class='postform'>
    	<option value="0">&nbsp;No Category</option>
    <?php
    echo get_the_tag_list('<option>','','</option>');
    ?>
    </select>
    <?php endwhile; else: ?>
    <?php endif; ?>

    But... it makes a different dropdown for every tag... so it fills up the page with dropdowns.

    I need to make this have all the tags in one dropdown. Also, I want this dropdown to be a selection box for searching.

    So, for example, there would be "dining", "auto", "movies" as tags. Then someone could choose "dining" in the dropdown box, type in "McDonald's" in the search box, and then find only posts that have "McDonald's" in the post as well as "dining" in the tags.

    Is this possible??

  3. noelgreen
    Member
    Posted 6 years ago #

    Closer still...

    I opened up my "category-template.php" file in the "wp-includes" folder and changed this...

    foreach ( $counts as $tag => $count ) {
    		$tag_id = $tag_ids[$tag];
    		$tag_link = clean_url($tag_links[$tag]);
    		$tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
    		$a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . attribute_escape( sprintf( __('%d topics'), $count ) ) . "'$rel style='font-size: " .
    			( $smallest + ( ( $count - $min_count ) * $font_step ) )
    			. "$unit;'>$tag</a>";
    	}
    
    	switch ( $format ) :
    	case 'array' :
    		$return =& $a;
    		break;
    	case 'list' :
    		$return = "<ul class='wp-tag-cloud'>\n\t<li>";
    		$return .= join("</li>\n\t<li>", $a);
    		$return .= "</li>\n</ul>\n";
    		break;
    	default :
    		$return = join("\n", $a);
    		break;
    	endswitch;

    To this...

    foreach ( $counts as $tag => $count ) {
    		$tag_id = $tag_ids[$tag];
    		$tag_link = clean_url($tag_links[$tag]);
    		$tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
    		$a[] = "<option value='$tag_id'>$tag</option>";
    	}
    
    	switch ( $format ) :
    	case 'array' :
    		$return =& $a;
    		break;
    	case 'list' :
    		$return = "<select name='tag' id='tag' class='postform'><option value='0'>&nbsp;All Categories</option>\n\t";
    		$return .= join("\n\t", $a);
    		$return .= "\n</select>\n";
    		break;
    	default :
    		$return = join("\n", $a);
    		break;
    	endswitch;

    This ALMOST works... but, alas, I get an ERROR 404 - Not Found.

    When I change the "$tag_id" to "$tag" inside the <option> field on the "$a[]" line I get results in the search, but they are all posts... not just those within that tag's realm.

    Any ideas??

  4. noelgreen
    Member
    Posted 6 years ago #

    Here's the code that works.
    Edit your "category-template.php" file in the wp-includes folder.

    foreach ( $counts as $tag => $count ) {
    		$tag_id = $tag_ids[$tag];
    		$tag_link = clean_url($tag_links[$tag]);
    		$tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
    		$a[] = "<option value='$tag'>$tag</option>";
    	}
    
    	switch ( $format ) :
    	case 'array' :
    		$return =& $a;
    		break;
    	case 'list' :
    		$return = "<select name='tag' class='postform'><option value='0'>&nbsp;All Categories</option>\n\t";
    		$return .= join("\n\t", $a);
    		$return .= "\n</select>\n";
    		break;
    	default :
    		$return = join("\n", $a);
    		break;
    	endswitch;
  5. coppertopjsq
    Member
    Posted 6 years ago #

    Hi. I tried the code but it just messed up my template. Do you have a working demo of this that I could look at? This is supposed to be the same as the dropdown category menu only with tags right?

    That would be so cool.

  6. coppertopjsq
    Member
    Posted 6 years ago #

    Hey I got a guy to write some code to make two drop down menus. One is static and the other is populated by all the tags minus the ones coded in the static menu. You can see it here

  7. CounterDax
    Member
    Posted 6 years ago #

    Thanks for not sharing.

  8. CounterDax
    Member
    Posted 6 years ago #

    The following does work... it's an adaptation of the tagcloud widget, rewritten to display a dropdown list. It needs some cleaning up but it worked in 2.3.2 (have not tried 2.5 yet):

    Just throw the following in your functions.php template file.

    function dropdown_tag_cloud( $args = '' ) {
    	$defaults = array(
    		'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
    		'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
    		'exclude' => '', 'include' => ''
    	);
    	$args = wp_parse_args( $args, $defaults );
    
    	$tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
    
    	if ( empty($tags) )
    		return;
    
    	$return = dropdown_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
    	if ( is_wp_error( $return ) )
    		return false;
    	else
    		echo apply_filters( 'dropdown_tag_cloud', $return, $args );
    }
    
    // $tags = prefetched tag array ( get_tags() )
    // $args['format'] = 'flat' => whitespace separated, 'list' => UL, 'array' => array()
    // $args['orderby'] = 'name', 'count'
    function dropdown_generate_tag_cloud( $tags, $args = '' ) {
    	global $wp_rewrite;
    	$defaults = array(
    		'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
    		'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
    	);
    	$args = wp_parse_args( $args, $defaults );
    	extract($args);
    
    	if ( !$tags )
    		return;
    	$counts = $tag_links = array();
    	foreach ( (array) $tags as $tag ) {
    		$counts[$tag->name] = $tag->count;
    		$tag_links[$tag->name] = get_tag_link( $tag->term_id );
    		if ( is_wp_error( $tag_links[$tag->name] ) )
    			return $tag_links[$tag->name];
    		$tag_ids[$tag->name] = $tag->term_id;
    	}
    
    	$min_count = min($counts);
    	$spread = max($counts) - $min_count;
    	if ( $spread <= 0 )
    		$spread = 1;
    	$font_spread = $largest - $smallest;
    	if ( $font_spread <= 0 )
    		$font_spread = 1;
    	$font_step = $font_spread / $spread;
    
    	// SQL cannot save you; this is a second (potentially different) sort on a subset of data.
    	if ( 'name' == $orderby )
    		uksort($counts, 'strnatcasecmp');
    	else
    		asort($counts);
    
    	if ( 'DESC' == $order )
    		$counts = array_reverse( $counts, true );
    
    	$a = array();
    
    	$rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
    
    	foreach ( $counts as $tag => $count ) {
    		$tag_id = $tag_ids[$tag];
    		$tag_link = clean_url($tag_links[$tag]);
    		$tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
    		$a[] = "\t<option value='$tag_link'>$tag ($count)</option>";
    	}
    
    	switch ( $format ) :
    	case 'array' :
    		$return =& $a;
    		break;
    	case 'list' :
    		$return = "<ul class='wp-tag-cloud'>\n\t<li>";
    		$return .= join("</li>\n\t<li>", $a);
    		$return .= "</li>\n</ul>\n";
    		break;
    	default :
    		$return = join("\n", $a);
    		break;
    	endswitch;
    
    	return apply_filters( 'dropdown_generate_tag_cloud', $return, $tags, $args );
    }
    ?>

    And wherever you want to display the dropdown list:

    <select name="tag-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
    	<option value="#">Most Used Tags </option>
    	<option value="<?php bloginfo('url'); ?>/?pagename=tags">All Tags</option>
    <?php dropdown_tag_cloud('number=10&orderby=count&order=DESC'); ?>
    </select>

    The function dropdown_tag_cloud() takes all the same arguments as tag_cloud() since it is actually a copy of tag_cloud() with just some minor changes.

  9. Sonika
    Member
    Posted 5 years ago #

    CounterDax, thanks!

  10. simplymother
    Member
    Posted 5 years ago #

    Anyone know if this works in version 2.6.3? I've searched for an answer and this is the only real solution I've found, but I'm hesitant to start editing templates, which I've never done, before I have a good idea it might work!

  11. simplymother
    Member
    Posted 5 years ago #

    YES! It does work. And I HAVE edited templates--I didn't realize it was just in my theme. I thought I was going to have to go into the wordpress files or something. This was easy.

    So, for the record, this code does work in 2.6.3. I was wondering if I needed that ?> at the end or if it is just what is already at the end of the functions.php template, and it turns out it is. So, you don't need two ?> at the end of the file.

    And I took out the lines
    '<option value="#">Most Used Tags </option>
    <option value="<?php bloginfo('url'); ?>/?pagename=tags">All Tags</option>'

    Since I didn't want those options. So far, everything appears to be working great. Thank you SOOO much CounterDax!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags