
Bug编译狮
在WordPress小工具中显示空分类可以通过以下步骤实现:
-
创建一个新的自定义小工具: 首先,你需要创建一个新的自定义小工具。这通常是在
wp-content/themes/your-theme/inc/widgets.php文件中完成的。 -
编写HTML和CSS: 在自定义小工具的PHP文件中,你可以添加一些简单的HTML和CSS来创建一个可以显示空分类的区域。这里我们使用了一个简单的列表来表示空分类。
-
设置样式表: 为这个小工具设置样式表,使其看起来更整洁。
-
激活小工具: 最后,确保将新的小工具添加到WordPress的后台并激活它。
下面是一个示例代码,展示如何在WordPress小工具中显示空分类:
<?php
/**
* 自定义小工具 - 显示空分类
*/
class EmptyCategoryWidget extends WP_Widget {
function __construct() {
parent::__construct(
'empty_category_widget', // Base ID
esc_html__( 'Empty Category Widget', 'text_domain' ), // Name
array( 'description' => esc_html__( 'Displays an empty category list.', 'text_domain' ) ) // Args
);
}
public function widget( $args, $instance ) {
extract($args);
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
if (!empty($title)) {
echo $before_title . $title . $after_title;
}
?>
<ul>
<?php
global $wpdb;
$categories = $wpdb->get_results("SELECT cat_ID FROM $wpdb->term_relationships WHERE term_taxonomy_id IN ( SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE t.slug = 'uncategorized' )");
foreach ($categories as $category) {
$cat_name = get_term_by('id', $category->cat_ID, 'category')->name;
echo "<li>" . $cat_name . "</li>";
}
?>
</ul>
<?php
echo $after_widget;
}
public function form( $instance ) {
$title = !empty( $instance[ 'title' ] ) ? $instance[ 'title' ] : esc_html__( 'New Title', 'text_domain' );
?>
<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 esc_attr( $title ); ?>">
</p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( !empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
register_widget( 'EmptyCategoryWidget' );
?>
使用步骤:
- 创建一个新的自定义小工具。
- 打开
inc/widgets.php文件(位于wp-content/themes/your-theme目录下)。 - 添加上述代码。
- 设置小工具的标题、描述等信息。
- 激活新创建的小工具。
效果:
当启用此小工具时,在页面或侧边栏中会显示一个空分类列表,例如“Uncategorized”类别。
请注意,这只是一个基本示例,实际应用可能需要根据具体需求进行调整。此外,确保在生产环境中测试所有功能以避免任何潜在的问题。

黑板前的Bug语法讲师
如何在 WordPress 小工具中显示空分类
在 WordPress 中,小工具是一种插件或扩展,可以添加额外的功能到网站上。一个常见的需求是在页面上展示一些特定类型的分类。例如,如果你有一个“产品”类别,你想在首页或其他页面上显示它,那么你可以创建一个自定义小工具来实现这个功能。
以下步骤将指导您如何在 WordPress 小工具中显示空分类:
1. 创建新插件
首先,你需要创建一个新的插件文件夹并在其中创建一个 functions.php 文件。在这个文件中,你会编写处理小工具和自定义功能的代码。
// functions.php
function my_custom_widget() {
$args = array(
'title' => 'My Custom Widget',
'description' => 'This is a custom widget for displaying categories.',
'show_in_rest' => true,
'icon_url' => 'dashicons-admin-generic'
);
register_sidebar( array(
'name' => 'Sidebar Widget Area',
'id' => 'sidebar-widget-area',
'before_title' => '<h4>',
'after_title' => '</h4>',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<span>',
'after_title' => '</span>',
'description' => '',
'classname' => 'widget-content',
'description_class' => '',
'instance' => 0,
) );
// Add a new category to the main post type.
wp_insert_term('my-custom-category', 'category');
return $args;
}
add_action( 'widgets_init', 'my_custom_widget' );
2. 添加小工具到主题
接下来,在您的主题文件中添加小工具。这里我们假设您的主题名称为 My Theme。
// theme.php
add_theme_support( 'post-thumbnails' );
register_nav_menus( array(
'primary' => __( 'Primary Menu' ),
) );
add_image_size( 'my-image-size', 800, 600, false );
register_sidebar( array(
'name' => __( 'Sidebar Widget Area' ),
'id' => 'sidebar-1',
'description' => __( 'Add widgets here.' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h3>',
'after_title' => '</h3>',
) );
3. 配置小工具选项
在您的主题配置文件中,您可以设置小工具的外观、行为等选项。
// theme_options.php
$theme_options = array(
'sidebars_widgets_count' => 5,
);
set_theme_mod( 'sidebars_widgets_count', $theme_options['sidebars_widgets_count'] );
4. 使用小工具
现在,当您激活并启用该插件时,它应该会自动注册到您的网站上。然后,您可以在任何需要的地方使用 get_categories() 函数来获取当前的所有分类,并将其显示在指定的小工具区域中。
// 示例:显示所有分类的列表
if ( ! function_exists( 'display_all_categories' ) ) :
/**
* Display all categories in list format.
*
* @since 1.0.0
*/
function display_all_categories() {
global $wp_query;
if ( empty( $wp_query->query_vars['cat'] ) ) {
return;
}
$categories = get_categories();
echo '<ul>';
foreach ( $categories as $category ) {
echo '<li><a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a></li>';
}
echo '</ul>';
}
add_action( 'template_redirect', 'display_all_categories' );
endif;
// 使用小工具
echo '<div id="my-custom-widget">';
echo '<h2>我的自定义小工具</h2>';
echo '<p>在这里添加您想要显示的分类。</p>';
echo '<form method="post">';
echo '<input type="hidden" name="action" value="insert_category">';
echo '<label for="category">选择分类:</label>';
echo '<select id="category" name="category">';
foreach ( get_categories() as $category ) {
echo '<option value="' . $category->term_id . '">' . $category->name . '</option>';
}
echo '</select>';
echo '<button type="submit">插入分类</button>';
echo '</form>';
echo '<pre>';
echo do_shortcode('[my_custom_widget]');
echo '</pre>';
echo '</div>';
以上就是在 WordPress 中创建一个自定义小工具以显示空分类的简单教程。通过上述步骤,您可以在不同的页面位置轻松地显示这些分类信息。

