[How-to] Mostrar los tags en menú desplegable

Tags Menús Despegables Desde que se comenzó a usar los tags, se ha revolucionado la forma de clasificar los artículos. Palabras claves que nos ayudan a encontrar el artículo que buscamos con mayor rapidez. OJO que los tags no vienen a reemplazar las categorias, sin embargo el uso de uno o del otro o de ambos, ya dependerá del mismo autor de blog.

La forma de mostrar los tags también es fundamental y una forma muy usada son las nubes de tags (un ejemplo pueden verlo en el sidebar al costado en este mismo blog). Pero esto a la vez puede ser aburrido es por ello que en Hack Wordpress, un blog que recomiendo para sacarle mejor el jugo a tu blog, han hecho un How-to donde explican muy bien como ordenar los tags dentro de un menú desplegable.

Lograrlo es muy simple, primero hay que crear una función php y para ello hay que copiar y pegar el siguiente código en tu archivo functions.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
function dropdown_tag_cloud( $args = '' ) {
	$defaults = array(
		'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
		'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
		'exclude' => '', 'include' => ''
	);
	$args = wtf_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_wtf_error( $return ) )
		return false;
	else
		echo apply_filters( 'dropdown_tag_cloud', $return, $args );
}
 
function dropdown_generate_tag_cloud( $tags, $args = '' ) {
	global $wtf_rewrite;
	$defaults = array(
		'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
		'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
	);
	$args = wtf_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_wtf_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($wtf_rewrite) && $wtf_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;', wtf_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 );
}
?>

Luego ubicar el lugar donde se querrá insertar el menú desplegable en el Theme y abriendo el archivo deseado, se insertará el código que llamará a la funcion php.

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

Espero que les sirva.

[Enlace: How to: Display your WordPress Tags in a Drop-Down Menu]

Saludos
The Ghost

The Ghost ha escrito este artículo el 09.30.2008 a las .
Mi nombre es Diego Castañeda Prado.

Artículos Relacionados



Dreamhost

Comenta!

Conectate con tu cuenta de Facebook y deja tu comentario en Tinta Fantasma