Виджет для отображения пользовательского облака тегов таксономии
Как изменить виджет по умолчанию для отображения облака тегов пользовательской таксономии?
2 answers
Не знаю ни одного, но вы можете легко создать свой собственный:
<?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'));
}
}
?>
Просто измените: "Заголовок вашего виджета" на ваш заголовок и "таксономия здесь" на название вашей таксономии.
Вы можете изменить внешний вид, передав больше аргументов в $cloud_args из большого списка в кодексе
Надеюсь, это поможет.
Существующий ответ здесь отличный, но, к сожалению, из-за возраста ответа он не работает для более новых версий WordPress.
Приведенный ниже код улучшается двумя способами:
1 - Это рекомендуемый/наилучший метод для более новых версий WordPress, начиная с версии 2.8
2 - Это позволяет вам выбирать таксономию через интерфейс виджета панели мониторинга, а не жестко ее кодировать.
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>';
}
}
Хотя технически вы могли бы просто добавить это относится к файлу функций вашей темы, я предпочитаю помещать его в отдельный файл темы (например, widgets.php
) и включать этот файл в файл функций.