[英]WP Widget Categories How to add a description of the number of articles to the widget?
I need to create custom categories widget for WordPress.我需要为 WordPress 创建自定义类别小部件。 It should look something like this.
它应该看起来像这样。
I found in this topic how to create custom categories but I can't understand how to add word - "articles" and remove brackets what we have in standard widget
我在本主题中找到了如何创建自定义类别,但我无法理解如何添加单词 - “文章”并删除标准小部件中的括号
code of my-category我的类别代码
<?php
class My_Widget_Categories extends WP_Widget {
function My_Widget_Categories() {
$widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "My list or dropdown of categories" ) );
$this->__construct('my_categories', __('My Categories'), $widget_ops);
}
// FRONTEND
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title']);
$c = $instance['count'] ? '1' : '0';
$h = $instance['hierarchical'] ? '1' : '0';
$d = $instance['dropdown'] ? '1' : '0';
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
$cat_args = array(
'orderby' => 'name',
'show_count' => $c,
'hierarchical' => $h
);
if ( $d ) {
$cat_args['show_option_none'] = __('Select Category');
wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args));
?>
<script>
var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
</script>
<?php
} else {
?>
<ul>
<?php
$cat_args['title_li'] = '';
wp_list_categories(apply_filters('widget_categories_args', $cat_args));
?>
</ul>
<?php
}
echo $after_widget;
}
/// Saving of settings
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['count'] = $new_instance['count'] ? 1 : 0;
$instance['hierarchical'] = $new_instance['hierarchical'] ? 1 : 0;
$instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
return $instance;
}
/// Backend of widget
function form( $instance ) {
//Defaults
$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
$title = esc_attr( $instance['title'] );
$count = isset($instance['count']) ? (bool) $instance['count'] :false;
$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
$dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
<label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Show as dropdown' ); ?></label><br />
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br />
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
<label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
<?php
}
}
Is it difficult to implement it in the form of a widget so that later it will be possible to expand the functionality ?以小部件的形式实现它是否很难,以便以后可以扩展功能?
get_terms returns a set of data, so in this data you would also find the total count of each term. get_terms返回一组数据,因此在此数据中您还可以找到每个术语的总数。
So you could do something like:所以你可以这样做:
$terms = get_terms( [
'taxonomy' => 'post_tag', // or the name of your taxonomy
'hide_empty' => false,
]);
when you would loop the $terms variable you will get an array with the following information:当您循环$terms变量时,您将获得一个包含以下信息的数组:
array(1) {
[0]=>
object(WP_Term) (11) {
["term_id"]=> //int
["name"]=> //string
["slug"]=> //string
["term_group"]=> //int
["term_taxonomy_id"]=> //int
["taxonomy"]=> //string
["description"]=> //string
["parent"]=> //int
["count"]=> // int
["filter"]=> //string
["meta"]=> array(0) { // presumably this would be some returned meta-data?
}
}
}
so in your loop, you could do something like所以在你的循环中,你可以做类似的事情
echo '<ul>';
foreach($terms as $term){
echo "<li>{$term->name} <span class='category-count'>{$term->count}</span></li>"
}
echo '</ul>;
Creating a widget is documented on the WordPress developers page: https://developer.wordpress.org/themes/functionality/widgets/创建小部件记录在 WordPress 开发人员页面上: https : //developer.wordpress.org/themes/functionality/widgets/
But if the document isn't clear, you could have a look at this tutorial on how to create a widget.但如果文档不清楚,您可以查看有关如何创建小部件的教程。 Doing this will give you the tools to make your own widget: https://www.hostinger.com/tutorials/how-to-create-custom-widget-in-wordpress
这样做将为您提供制作自己的小部件的工具: https : //www.hostinger.com/tutorials/how-to-create-custom-widget-in-wordpress
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.