【发布时间】:2019-12-02 11:30:48
【问题描述】:
我很难理解为什么类成员不能将分配/返回值从一种方法传递到另一种方法。
我应该如何使用类成员以便它可用 贯穿整个类和实例/对象。
var_dump($this->taxonomies) 返回NULL
class Btc_Taxonomy {
public $taxonomies;
public $taxonomy;
public function __construct() {
}
public function get_registered_taxonomies() {
$args = [];
$taxonomies_object = get_taxonomies( $args, 'objects' );
foreach ( $taxonomies_object as $taxonomy_object ) {
$this->taxonomies[ $taxonomy_object->name ]['name'] = $taxonomy_object->name;
$this->taxonomies[ $taxonomy_object->name ]['label'] = $taxonomy_object->label;
$this->taxonomies[ $taxonomy_object->name ]['hierarchical'] = $taxonomy_object->hierarchical;
}
return $this->taxonomies;
}
public function get_taxonomies_dropdown( $name = 'btc_taxonomies', $class = 'btc-dropdown', $echo = true ) {
$dropdown = '<select name="' . $name . '" class="' . $class . '">';
$dropdown .= '<option value="">Select a taxonomy</option>';
foreach ( $this->taxonomies as $taxonomy ) {
$dropdown .= '<option value="' . $taxonomy['name'] . '">' . $taxonomy['label'] . '</option>';
}
$dropdown .= '</select>';
if ( $echo ) {
echo $dropdown;
} else {
return $dropdown;
}
}
}
调试结果
这是$taxonomies_object 在get_registered_taxonomies() 中的输出
Array
(
[category] => WP_Taxonomy Object
(
[name] => category
[label] => Categories
[labels] => stdClass Object
(
[name] => Categories
[singular_name] => Category
[search_items] => Search Categories
[popular_items] =>
[all_items] => All Categories
[parent_item] => Parent Category
[parent_item_colon] => Parent Category:
[edit_item] => Edit Category
[view_item] => View Category
[update_item] => Update Category
[add_new_item] => Add New Category
[new_item_name] => New Category Name
[separate_items_with_commas] =>
[add_or_remove_items] =>
[choose_from_most_used] =>
[not_found] => No categories found.
[no_terms] => No categories
[items_list_navigation] => Categories list navigation
[items_list] => Categories list
[most_used] => Most Used
[back_to_items] => ← Back to Categories
[menu_name] => Categories
[name_admin_bar] => category
)
[description] =>
[public] => 1
[publicly_queryable] => 1
[hierarchical] => 1
[show_ui] => 1
[show_in_menu] => 1
[show_in_nav_menus] => 1
[show_tagcloud] => 1
[show_in_quick_edit] => 1
[show_admin_column] => 1
[meta_box_cb] => post_categories_meta_box
[meta_box_sanitize_cb] => taxonomy_meta_box_sanitize_cb_checkboxes
[object_type] => Array
(
[0] => post
)
[cap] => stdClass Object
(
[manage_terms] => manage_categories
[edit_terms] => edit_categories
[delete_terms] => delete_categories
[assign_terms] => assign_categories
)
[rewrite] => Array
(
[with_front] => 1
[hierarchical] => 1
[ep_mask] => 512
[slug] => category
)
[query_var] => category_name
[update_count_callback] =>
[show_in_rest] => 1
[rest_base] => categories
[rest_controller_class] => WP_REST_Terms_Controller
[_builtin] => 1
)
[post_tag] => WP_Taxonomy Object
(
[name] => post_tag
[label] => Tags
[labels] => stdClass Object
(
[name] => Tags
[singular_name] => Tag
[search_items] => Search Tags
[popular_items] => Popular Tags
[all_items] => All Tags
[parent_item] =>
[parent_item_colon] =>
[edit_item] => Edit Tag
[view_item] => View Tag
[update_item] => Update Tag
[add_new_item] => Add New Tag
[new_item_name] => New Tag Name
[separate_items_with_commas] => Separate tags with commas
[add_or_remove_items] => Add or remove tags
[choose_from_most_used] => Choose from the most used tags
[not_found] => No tags found.
[no_terms] => No tags
[items_list_navigation] => Tags list navigation
[items_list] => Tags list
[most_used] => Most Used
[back_to_items] => ← Back to Tags
[menu_name] => Tags
[name_admin_bar] => post_tag
)
[description] =>
[public] => 1
[publicly_queryable] => 1
[hierarchical] =>
[show_ui] => 1
[show_in_menu] => 1
[show_in_nav_menus] => 1
[show_tagcloud] => 1
[show_in_quick_edit] => 1
[show_admin_column] => 1
[meta_box_cb] => post_tags_meta_box
[meta_box_sanitize_cb] => taxonomy_meta_box_sanitize_cb_input
[object_type] => Array
(
[0] => post
)
[cap] => stdClass Object
(
[manage_terms] => manage_post_tags
[edit_terms] => edit_post_tags
[delete_terms] => delete_post_tags
[assign_terms] => assign_post_tags
)
[rewrite] => Array
(
[with_front] => 1
[hierarchical] =>
[ep_mask] => 1024
[slug] => tag
)
[query_var] => tag
[update_count_callback] =>
[show_in_rest] => 1
[rest_base] => tags
[rest_controller_class] => WP_REST_Terms_Controller
[_builtin] => 1
)
[nav_menu] => WP_Taxonomy Object
(
[name] => nav_menu
[label] => Navigation Menus
[labels] => stdClass Object
(
[name] => Navigation Menus
[singular_name] => Navigation Menu
[search_items] => Search Tags
[popular_items] => Popular Tags
[all_items] => Navigation Menus
[parent_item] =>
[parent_item_colon] =>
[edit_item] => Edit Tag
[view_item] => View Tag
[update_item] => Update Tag
[add_new_item] => Add New Tag
[new_item_name] => New Tag Name
[separate_items_with_commas] => Separate tags with commas
[add_or_remove_items] => Add or remove tags
[choose_from_most_used] => Choose from the most used tags
[not_found] => No tags found.
[no_terms] => No tags
[items_list_navigation] => Tags list navigation
[items_list] => Tags list
[most_used] => Most Used
[back_to_items] => ← Back to Tags
[menu_name] => Navigation Menus
[name_admin_bar] => Navigation Menu
[archives] => Navigation Menus
)
[description] =>
[public] =>
[publicly_queryable] =>
[hierarchical] =>
[show_ui] =>
[show_in_menu] =>
[show_in_nav_menus] =>
[show_tagcloud] =>
[show_in_quick_edit] =>
[show_admin_column] =>
[meta_box_cb] => post_tags_meta_box
[meta_box_sanitize_cb] => taxonomy_meta_box_sanitize_cb_input
[object_type] => Array
(
[0] => nav_menu_item
)
[cap] => stdClass Object
(
[manage_terms] => manage_categories
[edit_terms] => manage_categories
[delete_terms] => manage_categories
[assign_terms] => edit_posts
)
[rewrite] =>
[query_var] =>
[update_count_callback] =>
[show_in_rest] =>
[rest_base] =>
[rest_controller_class] =>
[_builtin] => 1
)
[link_category] => WP_Taxonomy Object
(
[name] => link_category
[label] => Link Categories
[labels] => stdClass Object
(
[name] => Link Categories
[singular_name] => Link Category
[search_items] => Search Link Categories
[popular_items] =>
[all_items] => All Link Categories
[parent_item] =>
[parent_item_colon] =>
[edit_item] => Edit Link Category
[view_item] => View Tag
[update_item] => Update Link Category
[add_new_item] => Add New Link Category
[new_item_name] => New Link Category Name
[separate_items_with_commas] =>
[add_or_remove_items] =>
[choose_from_most_used] =>
[not_found] => No tags found.
[no_terms] => No tags
[items_list_navigation] => Tags list navigation
[items_list] => Tags list
[most_used] => Most Used
[back_to_items] => ← Back to Link Categories
[menu_name] => Link Categories
[name_admin_bar] => Link Category
[archives] => All Link Categories
)
[description] =>
[public] =>
[publicly_queryable] =>
[hierarchical] =>
[show_ui] => 1
[show_in_menu] => 1
[show_in_nav_menus] =>
[show_tagcloud] => 1
[show_in_quick_edit] => 1
[show_admin_column] =>
[meta_box_cb] => post_tags_meta_box
[meta_box_sanitize_cb] => taxonomy_meta_box_sanitize_cb_input
[object_type] => Array
(
[0] => link
)
[cap] => stdClass Object
(
[manage_terms] => manage_links
[edit_terms] => manage_links
[delete_terms] => manage_links
[assign_terms] => manage_links
)
[rewrite] =>
[query_var] =>
[update_count_callback] =>
[show_in_rest] =>
[rest_base] =>
[rest_controller_class] =>
[_builtin] => 1
)
[post_format] => WP_Taxonomy Object
(
[name] => post_format
[label] => Formats
[labels] => stdClass Object
(
[name] => Formats
[singular_name] => Format
[search_items] => Search Tags
[popular_items] => Popular Tags
[all_items] => Formats
[parent_item] =>
[parent_item_colon] =>
[edit_item] => Edit Tag
[view_item] => View Tag
[update_item] => Update Tag
[add_new_item] => Add New Tag
[new_item_name] => New Tag Name
[separate_items_with_commas] => Separate tags with commas
[add_or_remove_items] => Add or remove tags
[choose_from_most_used] => Choose from the most used tags
[not_found] => No tags found.
[no_terms] => No tags
[items_list_navigation] => Tags list navigation
[items_list] => Tags list
[most_used] => Most Used
[back_to_items] => ← Back to Tags
[menu_name] => Formats
[name_admin_bar] => Format
[archives] => Formats
)
[description] =>
[public] => 1
[publicly_queryable] => 1
[hierarchical] =>
[show_ui] =>
[show_in_menu] =>
[show_in_nav_menus] => 1
[show_tagcloud] =>
[show_in_quick_edit] =>
[show_admin_column] =>
[meta_box_cb] => post_tags_meta_box
[meta_box_sanitize_cb] => taxonomy_meta_box_sanitize_cb_input
[object_type] => Array
(
[0] => post
)
[cap] => stdClass Object
(
[manage_terms] => manage_categories
[edit_terms] => manage_categories
[delete_terms] => manage_categories
[assign_terms] => edit_posts
)
[rewrite] => Array
(
[with_front] => 1
[hierarchical] =>
[ep_mask] => 0
[slug] => type
)
[query_var] => post_format
[update_count_callback] =>
[show_in_rest] =>
[rest_base] =>
[rest_controller_class] =>
[_builtin] => 1
)
[product_group] => WP_Taxonomy Object
(
[name] => product_group
[label] => Product Groups
[labels] => stdClass Object
(
[name] => Product Groups
[singular_name] => Product Group
[search_items] => Search Types
[popular_items] =>
[all_items] => All Product Groups
[parent_item] => Parent Product Group
[parent_item_colon] => Parent Product Group:
[edit_item] => Edit Product Group
[view_item] => View Category
[update_item] => Update Product Group
[add_new_item] => Make Product Group
[new_item_name] => New Product Group Name
[separate_items_with_commas] =>
[add_or_remove_items] =>
[choose_from_most_used] =>
[not_found] => No categories found.
[no_terms] => No categories
[items_list_navigation] => Categories list navigation
[items_list] => Categories list
[most_used] => Most Used
[back_to_items] => ← Back to Categories
[menu_name] => Product Groups
[name_admin_bar] => Product Group
[archives] => All Product Groups
)
[description] => Add product groups
[public] => 1
[publicly_queryable] => 1
[hierarchical] => 1
[show_ui] => 1
[show_in_menu] => 1
[show_in_nav_menus] =>
[show_tagcloud] =>
[show_in_quick_edit] => 1
[show_admin_column] => 1
[meta_box_cb] => post_categories_meta_box
[meta_box_sanitize_cb] => taxonomy_meta_box_sanitize_cb_checkboxes
[object_type] => Array
(
[0] => product
)
[cap] => stdClass Object
(
[manage_terms] => manage_categories
[edit_terms] => manage_categories
[delete_terms] => manage_categories
[assign_terms] => edit_posts
)
[rewrite] => Array
(
[with_front] => 1
[hierarchical] =>
[ep_mask] => 0
[slug] => product-group
)
[query_var] => product_group
[update_count_callback] =>
[show_in_rest] => 1
[rest_base] =>
[rest_controller_class] =>
[_builtin] =>
)
[job_type] => WP_Taxonomy Object
(
[name] => job_type
[label] => Types
[labels] => stdClass Object
(
[name] => Types
[singular_name] => Type
[search_items] => Search Types
[popular_items] => Popular Types
[all_items] => Types
[parent_item] => Parent Type
[parent_item_colon] => Parent Type:
[edit_item] => Edit Type
[view_item] => View Type
[update_item] => Update Type
[add_new_item] => Add New Type
[new_item_name] => New Type Name
[separate_items_with_commas] => Separate Types with commas
[add_or_remove_items] => Add or remove Types
[choose_from_most_used] => Choose from most used Types
[not_found] => No Types Found
[no_terms] => No categories
[items_list_navigation] => Categories list navigation
[items_list] => Categories list
[most_used] => Most Used
[back_to_items] => ← Back to Categories
[menu_name] => Types
[name_admin_bar] => Type
[archives] => Types
)
[description] =>
[public] => 1
[publicly_queryable] => 1
[hierarchical] => 1
[show_ui] => 1
[show_in_menu] => 1
[show_in_nav_menus] => 1
[show_tagcloud] => 1
[show_in_quick_edit] => 1
[show_admin_column] =>
[meta_box_cb] => post_categories_meta_box
[meta_box_sanitize_cb] => taxonomy_meta_box_sanitize_cb_checkboxes
[object_type] => Array
(
[0] => job
)
[cap] => stdClass Object
(
[manage_terms] => manage_categories
[edit_terms] => manage_categories
[delete_terms] => manage_categories
[assign_terms] => edit_posts
)
[rewrite] => Array
(
[with_front] =>
[hierarchical] =>
[ep_mask] => 0
[slug] => job_type
)
[query_var] => job_type
[update_count_callback] =>
[show_in_rest] =>
[rest_base] =>
[rest_controller_class] =>
[_builtin] =>
[show_tag_cloud] => 1
[sort] =>
)
)
这是get_registered_taxonomies() 中$this->taxonomies 在返回之前打印的输出。
Array
(
[category] => Array
(
[name] => category
[label] => Categories
[hierarchical] => 1
)
[post_tag] => Array
(
[name] => post_tag
[label] => Tags
[hierarchical] =>
)
[nav_menu] => Array
(
[name] => nav_menu
[label] => Navigation Menus
[hierarchical] =>
)
[link_category] => Array
(
[name] => link_category
[label] => Link Categories
[hierarchical] =>
)
[post_format] => Array
(
[name] => post_format
[label] => Formats
[hierarchical] =>
)
[product_group] => Array
(
[name] => product_group
[label] => Product Groups
[hierarchical] => 1
)
[job_type] => Array
(
[name] => job_type
[label] => Types
[hierarchical] => 1
)
)
这是我在打开大括号后在get_taxonomies_dropdown() { 中打印$this->tasonomies 时得到的结果
NULL
这是我在打开大括号后在get_taxonomies_dropdown() { 中打印$this->get_registered_taxonomies() 时得到的结果
Array
(
[category] => Array
(
[name] => category
[label] => Categories
[hierarchical] => 1
)
[post_tag] => Array
(
[name] => post_tag
[label] => Tags
[hierarchical] =>
)
[nav_menu] => Array
(
[name] => nav_menu
[label] => Navigation Menus
[hierarchical] =>
)
[link_category] => Array
(
[name] => link_category
[label] => Link Categories
[hierarchical] =>
)
[post_format] => Array
(
[name] => post_format
[label] => Formats
[hierarchical] =>
)
[product_group] => Array
(
[name] => product_group
[label] => Product Groups
[hierarchical] => 1
)
[job_type] => Array
(
[name] => job_type
[label] => Types
[hierarchical] => 1
)
)
【问题讨论】:
-
举一个不起作用的例子
-
get_registered_taxonomies()返回$this->taxonomies中的值,我在它下面的函数中调用该成员,称为get_taxonomies_dropdown(),它给出了NULL -
@B001ᛦ 它正在使用
add_action('admin_menu', ...)钩子加载到 WordPress 插件设置页面上。如果在 foreach 循环中使用该方法,它工作正常,但直接类成员给出NULL -
您的代码看起来不错。您确定 get_taxonomies() 正在返回值吗?
-
@user1309690 是的,它是返回值。我已经检查过,如果我在 foreach 而不是 member 中使用该方法,它正在工作。
标签: php class class-members