【问题标题】:Custom Post Type in Wordpress not behaving as Page or PostWordpress 中的自定义帖子类型不表现为页面或帖子
【发布时间】:2019-01-03 23:32:26
【问题描述】:

我使用此代码注册了一个自定义帖子类型:

    register_post_type( 'gallerydoc', array(
    'labels' => array(
        'name' => __( 'Gallery Docs' ),
        'singular_name' => __( 'Gallery Doc' )
    ),
    'public' => true,
    'publicly_queryable' => false,
    // '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
    // '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
    'capability_type' => 'page',
    'map_meta_cap' => true,
    'menu_position' => 20,
    'hierarchical' => true,
    'rewrite' => false,
    'query_var' => false,
    'delete_with_user' => true,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
    'show_in_rest' => true,
    'rest_base' => 'pages',
    'rest_controller_class' => 'WP_REST_Posts_Controller',
) );

我从注册页面类型的functions.php 复制了代码。

我唯一注释掉的是“仅供内部使用。请勿使用”的行。

但自定义类型的行为仍然不像页面或帖子类型。

自定义帖子类型:

  • 无法编辑永久链接
  • 在站点根目录下没有 slug
  • 没有选择模板页面

我读到自定义帖子类型不能将它们的 slug 直接放在根目录下:

Wordpress - Page and custom post type with this same slug

因为这会与页面和帖子发生冲突。但是,嘿,页面和帖子可以很好地相互配合。

那么为什么我的自定义帖子类型也不能这样做呢?

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    请设置'publicly_queryable' => true,'rewrite' => true, 以便您可以查看链接并进行编辑。

    还可以找到下面的代码。

    register_post_type( 'gallerydoc', array(
        'labels' => array(
            'name' => __( 'Gallery Docs' ),
            'singular_name' => __( 'Gallery Doc' )
        ),
        'public' => true,
        'publicly_queryable' => true,
        // '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
        // '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
        'capability_type' => 'page',
        'map_meta_cap' => true,
        'menu_position' => 20,
        'hierarchical' => true,
        'rewrite' => true,
        'query_var' => false,
        'delete_with_user' => true,
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
        'show_in_rest' => true,
        'rest_base' => 'pages',
        'rest_controller_class' => 'WP_REST_Posts_Controller',
    ) );
    

    【讨论】:

    • 这个答案并不能解决问题:1)在站点根目录下没有 slug 2)没有模板页面的选择
    • 1) 它将直接在站点根目录下显示 slug 2) 对于页面模板,您必须在活动主题中创建页面模板,然后它将显示页面模板选项。
    猜你喜欢
    • 2012-11-11
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    相关资源
    最近更新 更多