2017-06-28 47 views
-1

我想回收自定義帖子類型wordpress函數和add_action鉤子。不過,我不斷收到我的函數「cpt_function_name」缺少的參數。在這段代碼的底部,我嘗試傳遞實例化時需要傳遞給類的參數。幫幫我?在PHP類中爲函數傳遞外部參數

class classCPT 
{ 
    public $singular; 
    public $plural; 
    public $cpt_name; 
    public $cpt_icon; 

    public function __construct() { 
    add_action('admin_init', array($this, cpt_function_name)); 
    } 

    public function cpt_function_name($singular, $plural, $cpt_name, $cpt_icon) { 

    $labels = array(
     'name'       => $plural, 
     'singular_name'    => $singular, 
     'add_name'     => 'Add New', 
     'add_new_item'    => 'Add New ' . $singular, 
     'edit'       => 'Edit', 
     'edit_item'     => 'Edit ' . $singular, 
     'new_item'     => 'New ' . $singular, 
     'view'       => 'View ' . $singular, 
     'view_item'     => 'View ' . $singular, 
     'search_term'    => 'Search ' . $singular, 
     'parent'      => 'Parent ' . $singular, 
     'not_found'     => 'No ' . $plural . ' found', 
     'not_found_in_trash'  => 'No ' . $plural . ' in Trash', 
    ); 

    $args = array(
     'labels'    => $labels, 
     'public'      => true, 
     'publicly_queryable'  => true, //part of WP query 
     'exclude_from_search'  => false, 
     'show_in_nav_menus'  => true, 
     'show_ui'      => true, 
     'show_in_menu'    => true, 
     'show_in_admin_bar'  => true, 
     'menu_position'    => 20, 
     'menu_icon'     => $cpt_icon, 
     'can_export'     => true, 
     'delete_with_user'  => false, 
     'hierarchical'    => false, 
     'has_archive'     => true, 
     'query_var'     => true, 
     'capability_type'   => 'post', 
     'map_meta_cap'    => true, 
     'taxonomies'   => array(''), 
     'rewrite'      =>array('slug' => $cpt_name, 'with_front' => true, 'pages' => true, 'feeds' => true,), 
     'supports'     =>array('title', 'editor', 'thumbnail',), 
    ); 

    register_post_type($cpt_name , $args); 
    } 

} 

$newCPT = new classCPT('Safari', 'Safaris', 'safaris', 'dashicons-palmtree'); 

回答

1

對於添加PARAMS到ADD_ACTION呼叫,則該特定動作具有支持的話,這是不符合admin_init(https://codex.wordpress.org/Plugin_API/Action_Reference/admin_init)的情況。

這就是說,你必須在對象中設置變量,然後從你的公共函數調用它們。

與此

public function __construct($singular, $plural, $cpt_name, $cpt_icon) { 
    $this->singular = $singular; 
    $this->plural= $plural; 
    $this->cpt_name= $cpt_name; 
    $this->cpt_icon= $cpt_icon; 
    add_action('admin_init', array($this, 'cpt_function_name')); 
} 

,並與您cpt_function_name替換您_construct功能:

public function cpt_function_name() { 

$labels = array(
    'name'       => $this->plural, 
    'singular_name'    => $this->singular, 
    'add_name'     => 'Add New', 
    'add_new_item'    => 'Add New ' . $this->singular, 
    'edit'       => 'Edit', 
    'edit_item'     => 'Edit ' . $this->singular, 
    'new_item'     => 'New ' . $this->singular, 
    'view'       => 'View ' . $this->singular, 
    'view_item'     => 'View ' . $this->singular, 
    'search_term'    => 'Search ' . $this->singular, 
    'parent'      => 'Parent ' . $this->singular, 
    'not_found'     => 'No ' . $this->plural . ' found', 
    'not_found_in_trash'  => 'No ' . $this->plural . ' in Trash', 
); 

$args = array(
    'labels'    => $labels, 
    'public'      => true, 
    'publicly_queryable'  => true, //part of WP query 
    'exclude_from_search'  => false, 
    'show_in_nav_menus'  => true, 
    'show_ui'      => true, 
    'show_in_menu'    => true, 
    'show_in_admin_bar'  => true, 
    'menu_position'    => 20, 
    'menu_icon'     => $this->cpt_icon, 
    'can_export'     => true, 
    'delete_with_user'  => false, 
    'hierarchical'    => false, 
    'has_archive'     => true, 
    'query_var'     => true, 
    'capability_type'   => 'post', 
    'map_meta_cap'    => true, 
    'taxonomies'   => array(''), 
    'rewrite'      =>array('slug' => $this->cpt_name, 'with_front' => true, 'pages' => true, 'feeds' => true,), 
    'supports'     =>array('title', 'editor', 'thumbnail',), 
); 

register_post_type($this->cpt_name , $args);} 
+2

我想這是'陣列($此, 'cpt_function_name')',用引號。 –

+0

是。對不起,:)更新了答案 –