[英]Class member is null in another method returning value from the first one
我無法理解為什么類成員不能將分配/返回值從一種方法轉移到另一種方法。
我應該如何使用類成員,以便在整個類和實例/對象中都可用。
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;
}
}
}
這是get_registered_taxonomies()
$taxonomies_object
的輸出
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] =>
)
)
這是$this->taxonomies
在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_taxonomies_dropdown() {
剛打開大括號后打印$this->tasonomies
時得到的結果
NULL
這就是我在get_taxonomies_dropdown() {
剛打開大括號后打印$this->get_registered_taxonomies()
時get_taxonomies_dropdown() {
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->taxonomies
數組。 為了解決這個問題,你應該叫$this->get_registered_taxonomies()
建立下拉選項之前或添加調用$this->get_registered_taxonomies()
類的構造器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.