custom post type plugin error

时间:2021-05-07 作者:anna nguyen

我按照以下步骤为自定义帖子类型设置插件:https://kinsta.com/blog/wordpress-custom-post-types/#register

但我得到了错误:解析错误:语法错误,意外“”,$args);\'(T\\u CONSTANT\\u ENCAPSED\\u STRING),D:\\xampp\\htdocs\\simplepr\\wp content\\plugins\\goldnuggs custom post type\\goldnuggs cpt中应为“)”。php在线48

有人能看出我的错误吗?

 <?php
/*
Plugin Name: Register Custom Post Types

*/

function goldnuggs_register_post_type() {
    $labels = array(
 \'name\' => __( ‘Case-studies’, ‘goldnuggs’ ),
 \'singular_name\' => __( \'Case Study\', ‘goldnuggs’ ),
 \'add_new\' => __( \'New Case Study\', ‘goldnuggs’ ),
 \'add_new_item\' => __( \'Add New Case Study\', ‘goldnuggs’ ),
 \'edit_item\' => __( \'Edit Case Study\', ‘goldnuggs’ ),
 \'new_item\' => __( \'New Case Study\', ‘goldnuggs’ ),
 \'view_item\' => __( \'View Case Studies\', ‘goldnuggs’ ),
 \'search_items\' => __( \'Search Case Studies\', ‘goldnuggs’ ),
 \'not_found\' =>  __( \'No Case Studies Found\', ‘goldnuggs’ ),
 \'not_found_in_trash\' => __( \'No Case Studies found in Trash\', ‘goldnuggs’ ),
);

$args = array(
 \'labels\' => $labels,
 \'has_archive\' => true,
 \'public\' => true,
 \'hierarchical\' => false,
 \'supports\' => array(
  \'title\',
  \'editor\',
  \'excerpt\',
  \'custom-fields\',
  \'thumbnail\',
  \'page-attributes\'
 ),
 \'taxonomies\' => \'category\',
 \'rewrite\'   => array( \'slug\' => \'case-study\' ),
 ‘show_in_rest’ => true
);

register_post_type( ‘goldnugg_case\', $args );

}
add_action( \'init\', \'goldnuggs_register_post_type\' );
任何帮助都将不胜感激!

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

问题是您的代码使用了“卷轴引号”,这可能是教程cms自动转换的内容(WordPress…)。

‘show_in_rest’ => true
您需要将这些更改为单引号:

\'show_in_rest\' => true
完整代码:

<?php
/*
Plugin Name: Register Custom Post Types

*/

function goldnuggs_register_post_type() {
    $labels = array(
        \'name\' => __( \'Case-studies\', \'goldnuggs\' ),
        \'singular_name\' => __( \'Case Study\', \'goldnuggs\' ),
        \'add_new\' => __( \'New Case Study\', \'goldnuggs\' ),
        \'add_new_item\' => __( \'Add New Case Study\', \'goldnuggs\' ),
        \'edit_item\' => __( \'Edit Case Study\', \'goldnuggs\' ),
        \'new_item\' => __( \'New Case Study\', \'goldnuggs\' ),
        \'view_item\' => __( \'View Case Studies\', \'goldnuggs\' ),
        \'search_items\' => __( \'Search Case Studies\', \'goldnuggs\' ),
        \'not_found\' =>  __( \'No Case Studies Found\', \'goldnuggs\' ),
        \'not_found_in_trash\' => __( \'No Case Studies found in Trash\', \'goldnuggs\' ),
    );

    $args = array(
        \'labels\' => $labels,
        \'has_archive\' => true,
        \'public\' => true,
        \'hierarchical\' => false,
        \'supports\' => array(
            \'title\',
            \'editor\',
            \'excerpt\',
            \'custom-fields\',
            \'thumbnail\',
            \'page-attributes\'
        ),
        \'taxonomies\' => \'category\',
        \'rewrite\'   => array( \'slug\' => \'case-study\' ),
        \'show_in_rest\' => true
    );

    register_post_type( \'goldnugg_case\', $args );

}
add_action( \'init\', \'goldnuggs_register_post_type\' );