简体   繁体   English

破解WordPress类别小部件

[英]Hacking the WordPress Category Widget

The default WordPress categories widget does not allow excluding named categories. 默认的WordPress类别小部件不允许排除已命名的类别。

I've created a plugin which creates adds a Customized category widget to the "Available Widgets" listing which gives me some control over the items I want to exclude. 我已经创建了一个插件,该插件可以向“可用小部件”列表中添加一个自定义类别的小部件,这使我可以控制要排除的项目。 Code is below... 代码如下...

<?php
/*
Plugin Name: Custom Categories Widget
Plugin URI: http://mysite.com
Description: Removes the Specified Categories from the Default Categories Listing
Author: Me
Version: 1.0
Author URI: http://mysite.com
*/


function widget_my_categories() 
{
    wp_list_categories('exclude=1');
}

function my_categories_init()
{
  register_sidebar_widget(__('Custom Categories Widget'), 'widget_my_categories');
}

add_action("plugins_loaded", "my_categories_init");
?>

However, I want the generated code to emulate the same look and feel as the default categories widget (ie, the word "categories" appears as a bullet in my widget, but as an h4 level heading element in the default categories widget. I want the same structure to be applied to my custom widget as the default categories widget has. 但是,我希望生成的代码模仿默认类别小部件的外观(即,“类别”一词在我的小部件中显示为项目符号,但在默认类别小部件中显示为h4级别标题元素。我想要与默认类别小部件具有相同的结构,以应用于我的自定义小部件。

I'd also like to give the user the options to specify the title of the categories listing (just as they can do in the default categories widget). 我还想给用户提供选项来指定类别列表的标题(就像他们在默认类别小部件中所做的一样)。

btw, I'm using id 1 which is the default "uncategorized" category and assigning items to that category that I don't want to appear in the listing. 顺便说一句,我使用的是ID 1,它是默认的“未分类”类别,并将项目分配给我不想出现在清单中的那个类别。

Any help much appreciated! 任何帮助,不胜感激! :) :)

The code for the categories widget is in wp-includes/default_widgets.php . 类别小部件的代码在wp-includes/default_widgets.php Just copy the WP_Widget_Categories class, rename it and add your extra arguments to the wp_list_categories call. 只需复制WP_Widget_Categories类,对其进行重命名,然后将额外的参数添加到wp_list_categories调用中。

Alternatively you can modify the arguments for the wp_list_categories call in the widget using the widget_categories_args filter, although that will apply to all instances of the widget. 或者,您可以使用widget_categories_args过滤器修改窗口小部件中wp_list_categories调用的参数,尽管这将应用于窗口小部件的所有实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM