【问题标题】:Showing Notice when creating wordpress custom taxonomy创建 wordpress 自定义分类时显示通知
【发布时间】:2014-09-01 15:01:24
【问题描述】:

我正在尝试为我的自定义帖子创建自定义分类。但是当我尝试添加新类别时,会显示类似“尝试获取非对象属性的通知......”的通知。这是自定义帖子的代码

  function newsbox_post() {
    register_post_type( 'newsbox-post',
    array(
        'label'               => __( 'Newsbox Post', 'text_domain' ),
        'labels' => array(
            'name' => __( 'Newsbox Posts' ),
            'singular_name' => __( 'Newsbox Post' ),
            'menu_name'    => __( ' Newsbox Post', 'text_domain' ),
            'parent_item_colon'   => __( 'Parent  Newsbox post:', 'text_domain' ),
            'all_items'           => __( 'All  Newsbox post', 'text_domain' ),
            'view_item'           => __( 'View  Newsbox post', 'text_domain' ),
            'add_new_item'        => __( 'Add New  post', 'text_domain' ),
            'add_new'             => __( 'New post', 'text_domain' ),
            'edit_item'           => __( 'Edit post', 'text_domain' ),
            'update_item'         => __( 'Update post', 'text_domain' ),
            'search_items'        => __( 'Search post', 'text_domain' ),
            'not_found'           => __( 'No post found', 'text_domain' ),
            'not_found_in_trash'  => __( 'No post found in Trash', 'text_domain' )
        ),
        'public' => true,
        'rewrite' => array('slug' => 'Newsbox-post'),
        'description'         => __( 'Enter recent to your newsbox', 'text_domain' ),
        'supports'            => array( 'title', 'editor', 'page-attributes' ),
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => false,
        'show_in_admin_bar'   => true,
        'can_export'          => false,
        'has_archive'         => false,
        'exclude_from_search' => true,
        'publicly_queryable'  => true,
        'capability_type'     => 'post'
        )
  );
 }

add_action( 'init', 'newsbox_post' );

这是自定义分类的代码

 add_action( 'init', 'newsbox_post_category_taxonomy', 0 );

 function newsbox_post_category_taxonomy() {
 $labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Categories', 'taxonomy singular name' ),
'search_items' =>  __( 'Search Category' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ), 
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Categories Name' ),
'menu_name' => __( 'Categories' ),
 );     



  register_taxonomy('Categories',array('newsbox-post'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true

  ));

  }

请告诉我解决方案。谢谢。

【问题讨论】:

标签: wordpress custom-post-type


【解决方案1】:

您的问题与语法有关。您的代码存在两个问题

  • 切勿在自定义分类名称或自定义帖子类型名称中使用驼峰式。

  • 切勿在自定义帖子类型名称或自定义分类名称中使用连字符 (-),以及任何特殊字符。如果您必须在名称中分隔名称/单词,请仅使用下划线 (_)

Categories 应为 categories 用于您的分类名称,newsbox-post 应为 newsbox_post 用于您的自定义帖子类型名称

编辑

正确格式化您的代码并进行了相关更改。测试和工作

add_action( 'init', 'combined_registration' );

function combined_registration() {
    $labels = array(
        'name' => _x( 'Categories', 'taxonomy general name' ),
        'singular_name' => _x( 'Categories', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Category' ),
        'all_items' => __( 'All Categories' ),
        'parent_item' => __( 'Parent Category' ),
        'parent_item_colon' => __( 'Parent Category:' ),
        'edit_item' => __( 'Edit Category' ), 
        'update_item' => __( 'Update Category' ),
        'add_new_item' => __( 'Add New Category' ),
        'new_item_name' => __( 'New Categories Name' ),
        'menu_name' => __( 'Categories' ),
    );     

    register_taxonomy('categories',array('newsbox_post'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_admin_column' => true,
        'query_var' => true
        )
    );

    register_post_type( 'newsbox_post',
        array(
            'label'                 => __( 'Newsbox Post', 'text_domain' ),
            'labels'                => array(
            'name'                  => __( 'Newsbox Posts' ),
            'singular_name'         => __( 'Newsbox Post' ),
            'menu_name'             => __( ' Newsbox Post', 'text_domain' ),
            'parent_item_colon'     => __( 'Parent  Newsbox post:', 'text_domain' ),
            'all_items'             => __( 'All  Newsbox post', 'text_domain' ),
            'view_item'             => __( 'View  Newsbox post', 'text_domain' ),
            'add_new_item'          => __( 'Add New  post', 'text_domain' ),
            'add_new'               => __( 'New post', 'text_domain' ),
            'edit_item'             => __( 'Edit post', 'text_domain' ),
            'update_item'           => __( 'Update post', 'text_domain' ),
            'search_items'          => __( 'Search post', 'text_domain' ),
            'not_found'             => __( 'No post found', 'text_domain' ),
            'not_found_in_trash'    => __( 'No post found in Trash', 'text_domain' )
            ),
            'public'                => true,
            'rewrite'               => array('slug' => 'newsbox-post'),
            'description'           => __( 'Enter recent to your newsbox', 'text_domain' ),
            'supports'              => array( 'title', 'editor', 'page-attributes' ),
            'show_ui'               => true,
            'show_in_menu'          => true,
            'show_in_nav_menus'     => false,
            'show_in_admin_bar'     => true,
            'can_export'            => false,
            'has_archive'           => false,
            'exclude_from_search'   => true,
            'publicly_queryable'    => true,
            'capability_type'       => 'post'
        )
    );
}

【讨论】:

  • 更改但结果相同@Piter Goosen
  • 问题解决。但是当我尝试访问特征图像时,我面临另一个问题。你可以在这里查看dl.dropboxusercontent.com/u/168659703/Screenshot_1.png这里是代码$newsbox_post_img_src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '', false, '' );
  • 那你应该接受我的回答。至于您面临的其他问题,超出了这个问题的范围,您应该开始一个新问题:-)。谢谢
猜你喜欢
  • 2012-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多