wordpress中的过滤器类别

问题描述:

我想添加一个过滤器到get_categories函数。wordpress中的过滤器类别

我尝试这样做:

function wpr_cat_filter($args) { 
    $args['include'] = '37'; 
    return $args; 
} 
add_filter('get_categories','wpr_cat_filter'); 

,但似乎并不奏效。什么是错的任何标准?

+0

您是否已将它安装在wp-content/plugins中并在管理面板中将其激活?你如何测试这个? – MGwynne

+0

是的,插件位于插件目录中并被激活。为了测试这个,我创建了一个页面并且回显了get_categories()输出。 –

该过滤器实际上并未应用。

如果选中了 “get_categories” 功能(在WP-包括/ category.php),有没有应用 “get_categories” 过滤器:

function &get_categories($args = '') { 
     $defaults = array('taxonomy' => 'category'); 
     $args = wp_parse_args($args, $defaults); 

     $taxonomy = apply_filters('get_categories_taxonomy', $args['taxonomy'], $args); 

     // Back compat 
     if (isset($args['type']) && 'link' == $args['type']) { 
       _deprecated_argument(__FUNCTION__, '3.0', ''); 
       $taxonomy = $args['taxonomy'] = 'link_category'; 
     } 

     $categories = (array) get_terms($taxonomy, $args); 

     foreach (array_keys($categories) as $k) 
       _make_cat_compat($categories[$k]); 

     return $categories; 
} 

另外,如果你检查源:

wordpress$ grep -Ri "apply_filters" * | grep get_categories 
wp-includes/default-widgets.php:   wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args)); 
wp-includes/default-widgets.php:  wp_list_categories(apply_filters('widget_categories_args', $cat_args)); 
wp-includes/category.php: $taxonomy = apply_filters('get_categories_taxonomy', $args['taxonomy'], $args); 

也许它只是一个过滤器的占位符,您可以自己添加或稍后添加。

如果你想要的过滤器,改变get_category功能:

function &get_categories($args = '') { 
     $defaults = array('taxonomy' => 'category'); 
     $args = wp_parse_args($args, $defaults); 
     $args = apply_filters('get_categories', $args); 

     $taxonomy = apply_filters('get_categories_taxonomy', $args['taxonomy'], $args); 

     // Back compat 
     if (isset($args['type']) && 'link' == $args['type']) { 
       _deprecated_argument(__FUNCTION__, '3.0', ''); 
       $taxonomy = $args['taxonomy'] = 'link_category'; 
     } 

     $categories = (array) get_terms($taxonomy, $args); 

     foreach (array_keys($categories) as $k) 
       _make_cat_compat($categories[$k]); 

     return $categories; 
} 

您可能要提交错误与WordPress或询问他们的邮件列表,找出原因没有被应用此过滤器!