【问题标题】:WordPress Advanced Custom Post not displaying in backendWordPress 高级自定义帖子未显示在后端
【发布时间】:2016-11-19 15:21:12
【问题描述】:

这是代码,第 5 个也是最后一个没有显示在 ADMIN CP 面板中。所有其他人都工作正常,但是当我尝试添加第五个时。什么都没有出现。不确定是否有限制或其他东西,似乎不是我所阅读的。

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'Slides',
        array(
            'labels' => array(
                'name' => __( 'slides' ),
                'singular_name' => __( 'slide' )
            ),
            'public' => true,
            'has_archive' => true,
        )
    );

    register_post_type( 'Programs',
        array(
            'labels' => array(
                'name' => __( 'programs' ),
                'singular_name' => __( 'program' )
            ),
            'public' => true,
            'has_archive' => true,
        )
    );

    register_post_type( 'Boards',
        array(
            'labels' => array(
                'name' => __( 'Boards' ),
                'singular_name' => __( 'sponsor' )
            ),
            'public' => true,
            'has_archive' => true,
        )
    );

【问题讨论】:

  • 板子自定义帖子不显示在后端

标签: php arrays wordpress custom-post-type posts


【解决方案1】:

您的代码运行良好,您只是忘记了代码末尾的右括号 } 以及 slug post 类型中的一些错误(小写):

add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'slides', // <== here to lowercase (slug)
    array(
      'labels' => array(
        'name' => __( 'Slides' ), // <== here 1st letter uppercase
        'singular_name' => __( 'Slide' ) // <== here 1st letter uppercase
      ),
      'public' => true,
      'has_archive' => true,
    )
  );

    register_post_type( 'programs', // <== here to lowercase (slug)
    array(
      'labels' => array(
        'name' => __( 'Programs' ), // <== here 1st letter uppercase
        'singular_name' => __( 'Program' ) // <== here 1st letter uppercase
      ),
      'public' => true,
      'has_archive' => true,
    )
  );

    register_post_type( 'boards', // <== here to lowercase (slug)
    array(
      'labels' => array(
        'name' => __( 'Boards' ), 
        'singular_name' => __( 'Board' )  // <== here singular
      ),
      'public' => true,
      'has_archive' => true,
    )
  );
} // <== forgetted this

我已经在一个测试网站上测试了你的代码,甚至你的自定义帖子“Boards”正在显示和工作:

您可能需要通过后台 永久链接 设置来刷新重写规则,然后只需单击保存以重新生成与此新帖子类型相关的 Wordpress 重写规则……


WordPress 中的帖子类型有限制吗?
不同自定义帖子的数量没有限制。试试下面的例子,它以非常紧凑的代码生成 10 个带有 for 循环的自定义帖子:

add_action( 'init', 'create_post_type' );
function create_post_type() {
    $arr = array('abcdef','bcdefg','cdefgh','defghi','efghij','fghijk','ghijkl','hijklm','ijklmn','jklmno');
    for( $i = 0; $i < 10; $i++ ) {
        $slug = $arr[$i]; 
        $slugs = $arr[$i].'s';
        register_post_type( $slug,
            array( 
                'labels' => array(
                    'name' => $slugs,
                    'singular_name' => $slug ),
                'public' => true,
                'has_archive' => true ) 
        );
    }
}

参考资料:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多