如何在 start_el() 函数中挂接 Walker_Category_Checklist 参数?
How to hook Walker_Category_Checklist parameters in start_el() function?
我正在尝试扩展 Walker_Category_Checklist
class。
class My_Walker_Category_Checklist extends Walker_Category_Checklist {
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
var_dump( $args['MY_PAREMETER'] ); // Output is NULL...
var_dump( $args['checked_ontop'] ); // This is NULL too...
}
}
我需要将一些额外的参数传递给 $args
数组。此参数基于 post 元数据,如果我将在 start_el
中调用 get_post_meta()
,它将针对列表中的每个元素执行,这不好,因为元素数接近 500。
我在这里为 wp_terms_checklist_args
:
创建了钩子
add_filter( 'wp_terms_checklist_args', function( $args, $post_id ) {
if ( is_admin() ) {
if ( !empty( $args['taxonomy'] ) && ( $args['taxonomy'] === 'my-taxonomy' ) && ( ! isset( $args['walker'] ) || ! $args['walker'] instanceof Walker ) ) {
$args['walker'] = new My_Walker_Category_Checklist;
$args['MY_PAREMETER'] = get_post_meta( $post_id, 'my_data', 1 );
$args['checked_ontop'] = false;
}
}
return $args;
}, 10, 2 );
$args['checked_ontop'] = false
这个参数有效,但它在 start_el
处是 NULL
,所以我知道这是不同的 $args
参数。
如何将附加数据传递给扩展 class 中 start_el
函数的 $args
参数?
谢谢!
更新 1
将我的参数添加到 $args
后直接从 wp_terms_checklist_args
过滤 $args
的 var_dump
array(5) {
["taxonomy"]=>
string(14) "my-taxonomy"
["popular_cats"]=>
array(10) {
[0]=>
int(64)
//...
}
["walker"]=>
object(My_Walker_Category_Checklist)#3282 (4) {
["tree_type"]=>
string(8) "category"
["db_fields"]=>
array(2) {
["parent"]=>
string(6) "parent"
["id"]=>
string(7) "term_id"
}
["max_pages"]=>
int(1)
["has_children"]=>
NULL
}
["my_parameter"]=>
string(7) "my-data"
["checked_ontop"]=>
bool(false)
}
这是 var_dump
的 $args
来自 My_Walker_Category_Checklist
start_el
函数。这里没有在过滤器中添加的参数。
array(6) {
["taxonomy"]=>
string(14) "my-taxonomy"
["disabled"]=>
bool(false)
["list_only"]=>
bool(false)
["selected_cats"]=>
array(10) {
[0]=>
int(212)
//...
}
["popular_cats"]=>
array(3) {
[0]=>
int(64)
//...
}
["has_children"]=>
bool(true)
}
更新 1.1
以下一种方式将 args 传递给 wp_terms_checklist_args
什么都不给:
$args['selected_cats']['custom_data'] = array(
'MY_PAREMETER' => 'wow!',
);
这会在保存所选术语时出现问题,因为 selected_cats
变量已被重写。 var_dump
接下来给出:
["selected_cats"]=>
array(1) {
["custom_data"]=>
array(1) {
["MY_PAREMETER"]=>
string(4) "wow!"
}
}
所有选定的类别都被遗漏了。
我认为最好的解决方案是将需要的数据添加到子构造函数中。在创建class
时会调用一次。
class My_Walker_Category_Checklist extends Walker_Category_Checklist {
function __construct(){
$this->myparam = 'my param';
}
//...
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
var_dump( $this->myparam );
}
}
我正在尝试扩展 Walker_Category_Checklist
class。
class My_Walker_Category_Checklist extends Walker_Category_Checklist {
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
var_dump( $args['MY_PAREMETER'] ); // Output is NULL...
var_dump( $args['checked_ontop'] ); // This is NULL too...
}
}
我需要将一些额外的参数传递给 $args
数组。此参数基于 post 元数据,如果我将在 start_el
中调用 get_post_meta()
,它将针对列表中的每个元素执行,这不好,因为元素数接近 500。
我在这里为 wp_terms_checklist_args
:
add_filter( 'wp_terms_checklist_args', function( $args, $post_id ) {
if ( is_admin() ) {
if ( !empty( $args['taxonomy'] ) && ( $args['taxonomy'] === 'my-taxonomy' ) && ( ! isset( $args['walker'] ) || ! $args['walker'] instanceof Walker ) ) {
$args['walker'] = new My_Walker_Category_Checklist;
$args['MY_PAREMETER'] = get_post_meta( $post_id, 'my_data', 1 );
$args['checked_ontop'] = false;
}
}
return $args;
}, 10, 2 );
$args['checked_ontop'] = false
这个参数有效,但它在 start_el
处是 NULL
,所以我知道这是不同的 $args
参数。
如何将附加数据传递给扩展 class 中 start_el
函数的 $args
参数?
谢谢!
更新 1
将我的参数添加到 $args
wp_terms_checklist_args
过滤 $args
的 var_dump
array(5) {
["taxonomy"]=>
string(14) "my-taxonomy"
["popular_cats"]=>
array(10) {
[0]=>
int(64)
//...
}
["walker"]=>
object(My_Walker_Category_Checklist)#3282 (4) {
["tree_type"]=>
string(8) "category"
["db_fields"]=>
array(2) {
["parent"]=>
string(6) "parent"
["id"]=>
string(7) "term_id"
}
["max_pages"]=>
int(1)
["has_children"]=>
NULL
}
["my_parameter"]=>
string(7) "my-data"
["checked_ontop"]=>
bool(false)
}
这是 var_dump
的 $args
来自 My_Walker_Category_Checklist
start_el
函数。这里没有在过滤器中添加的参数。
array(6) {
["taxonomy"]=>
string(14) "my-taxonomy"
["disabled"]=>
bool(false)
["list_only"]=>
bool(false)
["selected_cats"]=>
array(10) {
[0]=>
int(212)
//...
}
["popular_cats"]=>
array(3) {
[0]=>
int(64)
//...
}
["has_children"]=>
bool(true)
}
更新 1.1
以下一种方式将 args 传递给 wp_terms_checklist_args
什么都不给:
$args['selected_cats']['custom_data'] = array(
'MY_PAREMETER' => 'wow!',
);
这会在保存所选术语时出现问题,因为 selected_cats
变量已被重写。 var_dump
接下来给出:
["selected_cats"]=>
array(1) {
["custom_data"]=>
array(1) {
["MY_PAREMETER"]=>
string(4) "wow!"
}
}
所有选定的类别都被遗漏了。
我认为最好的解决方案是将需要的数据添加到子构造函数中。在创建class
时会调用一次。
class My_Walker_Category_Checklist extends Walker_Category_Checklist {
function __construct(){
$this->myparam = 'my param';
}
//...
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
var_dump( $this->myparam );
}
}