Display wordpress Tags In Dropdown Menu without plugin

Display wordpress Tags In Dropdown Menu without plugin

WordPress tutorial, Display wordpress Tags In Dropdown Menu without plugin. we given simple code here which you need to put in theme functions.php file.

If you want to display the tags in drop down menu then use following code in functions.php file.

Display wordpress Tags In Dropdown Menu without plugin

01<?php
02function dropdown_tag_cloud( $args = '' ) {
03$defaults = array(
04'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
05'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
06'exclude' => '', 'include' => ''
07);
08$args = wp_parse_args( $args, $defaults );
09 
10$tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
11 
12if ( empty($tags) )
13return;
14 
15$return = dropdown_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
16if ( is_wp_error( $return ) )
17return false;
18else
19echo apply_filters( 'dropdown_tag_cloud', $return, $args );
20}
21 
22function dropdown_generate_tag_cloud( $tags, $args = '' ) {
23global $wp_rewrite;
24$defaults = array(
25'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
26'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
27);
28$args = wp_parse_args( $args, $defaults );
29extract($args);
30 
31if ( !$tags )
32return;
33$counts = $tag_links = array();
34foreach ( (array) $tags as $tag ) {
35$counts[$tag->name] = $tag->count;
36$tag_links[$tag->name] = get_tag_link( $tag->term_id );
37if ( is_wp_error( $tag_links[$tag->name] ) )
38return $tag_links[$tag->name];
39$tag_ids[$tag->name] = $tag->term_id;
40}
41 
42$min_count = min($counts);
43$spread = max($counts) - $min_count;
44if ( $spread <= 0 )
45$spread = 1;
46$font_spread = $largest - $smallest;
47if ( $font_spread <= 0 )
48$font_spread = 1;
49$font_step = $font_spread / $spread;
50 
51// SQL cannot save you; this is a second (potentially different) sort on a subset of data.
52if ( 'name' == $orderby )
53uksort($counts, 'strnatcasecmp');
54else
55asort($counts);
56 
57if ( 'DESC' == $order )
58$counts = array_reverse( $counts, true );
59 
60$a = array();
61 
62$rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
63 
64foreach ( $counts as $tag => $count ) {
65$tag_id = $tag_ids[$tag];
66$tag_link = clean_url($tag_links[$tag]);
67$tag = str_replace(' ', ' ', wp_specialchars( $tag ));
68$a[] = "\t<option value='$tag_link'>$tag ($count)</option>";
69}
70 
71switch ( $format ) :
72case 'array' :
73$return =& $a;
74break;
75case 'list' :
76$return = "<ul class='wp-tag-cloud'>\n\t<li>";
77$return .= join("</li>\n\t<li>", $a);
78$return .= "</li>\n</ul>\n";
79break;
80default :
81$return = join("\n", $a);
82break;
83endswitch;
84 
85return apply_filters( 'dropdown_generate_tag_cloud', $return, $tags, $args );
86}
87?>

In footer or sidebar file or where you want to display the tags in dropdown use the following code.

Display wordpress Tags In Dropdown Menu without plugin
Display wordpress Tags In Dropdown Menu without plugin
1<select name="tag-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
2<option value="#">Liste d'auteurs</option>
3<?php dropdown_tag_cloud('number=0&order=asc'); ?>
4</select>

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.