¿Cómo modificar el widget predeterminado para mostrar una nube de etiquetas de una taxonomía personalizada?
¿Cómo modificar el widget predeterminado para mostrar una nube de etiquetas de una taxonomía personalizada?
No conoces ninguno, pero puedes crear el tuyo fácilmente:
<?php
add_action("widgets_init", array('Widget_Custom_tax_tag_cloud', 'register'));
class Widget_Custom_tax_tag_cloud {
function control(){
echo 'No control panel';
}
function widget($args){
echo $args['before_widget'];
echo $args['before_title'] . 'Your widget title' . $args['after_title'];
$cloud_args = array('taxonomy' => 'Your taxonomy here');
wp_tag_cloud( $cloud_args );
echo $args['after_widget'];
}
function register(){
register_sidebar_widget('Widget name', array('Widget_Custom_tax_tag_cloud', 'widget'));
register_widget_control('Widget name', array('Widget_Custom_tax_tag_cloud', 'control'));
}
}
?>
simplemente cambia: 'El título de tu widget' con tu título y Tu 'taxonomía aquí' con el nombre de tu taxonomía.
puede cambiar la apariencia pasando más argumentos en $ cloud_args de la lista grande en códice
Espero que esto ayude.
La respuesta existente aquí es excelente, pero desafortunadamente, debido a la edad de la respuesta, no funciona para las versiones más nuevas de WordPress.
El siguiente código mejora de dos maneras:
1: es el método recomendado / recomendado para las nuevas versiones de WordPress, desde versión 2.8
2: le permite seleccionar la taxonomía a través de la interfaz del widget del tablero de mandos, en lugar de tenerla codificada.
add_action( 'widgets_init', 'custom_register_plugin_widget' );
function custom_register_plugin_widget() {
register_widget( 'Widget_Custom_Tax_Tag_Cloud' );
}
/**
* New "best practice" is to extend the built-in WP_Widget class
*
* Class Widget_Custom_tax_tag_cloud
*/
class Widget_Custom_Tax_Tag_Cloud extends WP_Widget {
function __construct() {
parent::__construct( 'custom_tax_tag_cloud', 'Custom Taxonomy Tag Cloud', array( 'description' => 'Display a tag cloud for a custom taxonomy.' ) );
}
/**
* Allows for manipulation, calculation, etc. when saving the widget instance in the dashboard.
*
* @param array $new_instance
* @param array $old_instance
*
* @return array
*/
function update( $new_instance, $old_instance ) {
return $new_instance;
}
/**
* Echos the widget contents in a sidebar
*
* @param array $args - the general widget arguments
* @param array $instance - the settings for this specific widget
*/
function widget( $args, $instance ) {
echo $args['before_widget'];
echo $args['before_title'] . 'Your widget title' . $args['after_title'];
$cloud_args = array( 'taxonomy' => 'catalogtag' );
wp_tag_cloud( $cloud_args );
echo $args['after_widget'];
}
/**
* Render the "Controls" in the dashboard menu under Appearance => Widgets
*
* @param array $instance - the settings for this instance of the widget
*
* @return null
*/
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'taxonomy' => 'post_tag' ) );
// Load the list of taxonomies available
$taxonomies = get_taxonomies( array( 'public' => TRUE , 'show_tagcloud' => TRUE), 'objects' );
echo '<p><label>Title</label><input name="' . $this->get_field_name( 'title' ) . '" id="' . $this->get_field_id( 'title' ) . '" value="' . esc_attr( $instance['title'] ) . '" /></p>';
echo '<p><label>Taxonomy</label><select name="' . $this->get_field_name('taxonomy') . ' id="' . $this->get_field_id('taxonomy') . '">';
echo '<option value="">Select Taxonomy...</option>';
foreach($taxonomies AS $tax) {
echo '<option value="' . $tax->name . '"';
echo ($tax->name == $instance['taxonomy']) ? ' selected' : '';
echo '>';
echo ( ! empty($tax->labels->singular_name)) ? $tax->labels->singular_name : $tax->label;
echo '</option>';
}
echo '</select></p>';
}
}
Aunque, técnicamente, puedes agregar esto al archivo de funciones de tu tema, tiendo a preferir ponerlo en un archivo de tema separado (como widgets.php
, e incluir ese archivo en el archivo de funciones).
Lea otras preguntas en las etiquetas custom-taxonomy taxonomy tags widgets