为什么在添加功能后我的自定义帖子类型没有显示?

时间:2020-10-21 作者:Tom

我正在尝试添加一个具有功能的自定义帖子类型,以便自定义用户角色可以对其执行某些操作。最终,自定义角色是一个客户端,发布一个网站,我希望客户端能够登录并查看其网站。然而,当我添加capabilities => \'website\'. 为什么没有出现?我在跟踪this tutorial 在这里,它似乎在3点54分起作用(车辆是一种定制的立柱类型)。

<?php

// Creating the website post type
function add_website_post_type() {

  $website_post_type_args = array(
    \'labels\' => $website_post_type_labels,
    \'public\' => true,
    \'menu_position\' => 5,
    \'menu_icon\' => \'dashicons-feedback\',
    \'hierarchical\' => false,
    \'supports\' => array( \'title\' ),
    \'has_archive\' => true,
    \'capability_type\' => \'website\'
  );

  // Define all the labels
  $website_post_type_labels = array(
    \'name\' => __( \'Websites\' ),
    \'singular_name\' => __( \'Website\' ),
    \'add_new_item\' => __( \'Add New Website\' ),
    \'edit_item\' => __( \'Edit Website\' ),
    \'new_item\' => __( \'New Website\' ),
    \'view_item\' => __( \'View Website\' ),
    \'view_items\' => __( \'View Websites\' ),
    \'not_found\' => __( \'No Websites Found\' ),
    \'not_found_in_trash\' => __( \'No Deleted Websites Found\' ),
    \'all_items\' => __( \'All Websites\' ),
    \'archives\' => __( \'Website Archives\' ),
    \'insert_into_item\' => __( \'Insert Into Website\' ),
    \'uploaded_to_this_item\' => __( \'Uploaded To This Website\' )
  );

  register_post_type( \'website\', $website_post_type_args );

}

add_action( \'init\', \'add_website_post_type\', 10 );

// Create the client user role
function add_client_user_role() {

  $capabilities = array(
    \'publish_website\'        => true,
    \'edit_website\'           => true,
    \'edit_others_website\'    => true,
    \'delete_website\'         => true,
    \'delete_others_website\'  => true,
    \'read_website\'           => true,
    \'read_private_website\'   => true,
    \'read\'
  );

  add_role( \'client\', \'Client\', $capabilities );

}

add_action( \'init\', \'add_client_user_role\', 10 );

1 个回复
最合适的回答,由SO网友:Awais 整理而成

您必须注册自定义功能,然后在register post type函数中应用

function register_website_caps() {

    $admins = get_role( \'administrator\' );

    $admins->add_cap( \'edit_website\' ); 
    $admins->add_cap( \'read_website\' ); 
    $admins->add_cap( \'delete_website\' ); 
    $admins->add_cap( \'edit_websites\' ); 
    $admins->add_cap( \'edit_others_websites\' ); 
    $admins->add_cap( \'publish_websites\' ); 
    $admins->add_cap( \'read_private_websites\' ); 
}
add_action( \'admin_init\', \'register_website_caps\');


// Creating the website post type
function add_website_post_type() {

   // Custom capabilities mapping
    $capabilities = array(
        \'edit_post\'             => \'edit_website\',
        \'read_post\'             => \'read_website\',
        \'delete_post\'           => \'delete_website\',
        \'edit_posts\'            => \'edit_websites\',
        \'edit_others_posts\'     => \'edit_others_websites\',
        \'publish_posts\'         => \'publish_websites\',
        \'read_private_posts\'    => \'read_private_websites\',
    );


  // Define all the labels
  $website_post_type_labels = array(
    \'name\' => __( \'Websites\' ),
    \'singular_name\' => __( \'Website\' ),
    \'add_new_item\' => __( \'Add New Website\' ),
    \'edit_item\' => __( \'Edit Website\' ),
    \'new_item\' => __( \'New Website\' ),
    \'view_item\' => __( \'View Website\' ),
    \'view_items\' => __( \'View Websites\' ),
    \'not_found\' => __( \'No Websites Found\' ),
    \'not_found_in_trash\' => __( \'No Deleted Websites Found\' ),
    \'all_items\' => __( \'All Websites\' ),
    \'archives\' => __( \'Website Archives\' ),
    \'insert_into_item\' => __( \'Insert Into Website\' ),
    \'uploaded_to_this_item\' => __( \'Uploaded To This Website\' )
  );

  $website_post_type_args = array(
    \'labels\' => $website_post_type_labels,
    \'public\' => true,
    \'menu_position\' => 5,
    \'menu_icon\' => \'dashicons-feedback\',
    \'hierarchical\' => false,
    \'supports\' => array( \'title\' ),
    \'has_archive\' => true,
    \'capabilities\' => $capabilities,
    \'map_meta_cap\' => true //Whether to use the internal default meta capability handling.
  );


  register_post_type( \'website\', $website_post_type_args );

}

add_action( \'init\', \'add_website_post_type\', 10 );

相关推荐

正在尝试获取wp-includes/capabilities.php中非对象的属性

在调试中,我每分钟都会收到以下通知序列。日志:[23-Oct-2012 13:27:33 UTC] PHP Notice: Trying to get property of non-object in mysite/wp-includes/capabilities.php on line 1022 [23-Oct-2012 13:27:33 UTC] PHP Notice: Trying to get property of non-object in mysite/wp-includes/