programing

WP REST API를 사용하여 사용자 지정 게시 유형에서 데이터를 가져오려면 어떻게 해야 합니까?

lovejava 2023. 9. 21. 20:00

WP REST API를 사용하여 사용자 지정 게시 유형에서 데이터를 가져오려면 어떻게 해야 합니까?

WP REST API를 사용하여 사용자 지정 게시물 유형에서 데이터를 얻고 싶습니다.

사용자 지정 게시물 유형은 "결과"이며 이 매개 변수를 사용하여 시도했습니다.

http://ejobbox.com/wp-json/wp/v2/result/?

http://ejobbox.com/wp-json/wp/v2/result/?per_page=10

에도.

http://ejobbox.com/wp-json/wp/v2/posts/?post_type=result

하지만 커스텀 포스트 타입으로 데이터를 받을 수 없습니다.

사용자 지정 게시물 유형에도 추가했습니다.

'show_in_rest'=> true,
'rest_base'          => 'result',
'rest_controller_class' => 'WP_REST_Posts_Controller',

그래도 저는 아무런 결과도 얻지 못했습니다.

제가 여기서 무엇을 잘못하고 있는지, 제가 주문한 우편물 종류에서 자료를 받으려면 어떻게 해야 하는지 알려주실 수 있나요?이것에 대한 제안을 부탁드립니다.

내 Function.php(Custom post type) 코드는 다음과 같습니다.

function codexres_custom_init() {
    $args = array(
      'public' => true,
      'label'  => 'Result'
    );
    register_post_type( 'result', $args );
}
add_action( 'init', 'codexres_custom_init' );


function codex_result_init() {
    $labels = array(
        'name'               => _x( 'Result', 'post type general name', 'your-plugin-textdomain' ),
        'singular_name'      => _x( 'Result', 'post type singular name', 'your-plugin-textdomain' ),
        'menu_name'          => _x( 'Result', 'admin menu', 'your-plugin-textdomain' ),
        'name_admin_bar'     => _x( 'Result', 'add new on admin bar', 'your-plugin-textdomain' ),
        'add_new'            => _x( 'Add New', 'Result', 'your-plugin-textdomain' ),
        'add_new_item'       => __( 'Add New Result', 'your-plugin-textdomain' ),
        'new_item'           => __( 'New Result', 'your-plugin-textdomain' ),

        'edit_item'          => __( 'Edit Result', 'your-plugin-textdomain' ),

        'view_item'          => __( 'View Result', 'your-plugin-textdomain' ),

        'all_items'          => __( 'All Result', 'your-plugin-textdomain' ),

        'search_items'       => __( 'Search Result', 'your-plugin-textdomain' ),

        'parent_item_colon'  => __( 'Parent Result:', 'your-plugin-textdomain' ),
        'not_found'          => __( 'No Result found.', 'your-plugin-textdomain' ),
        'not_found_in_trash' => __( 'No Result found in Trash.', 'your-plugin-textdomain' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'your-plugin-textdomain' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'show_in_rest'       => true,
        'query_var'          => true,
        'menu_icon'          => 'dashicons-admin-users',
        'rewrite' => array( 'slug' => __('result', 'result')),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'rest_base'          => 'result',
        'rest_controller_class' => 'WP_REST_Posts_Controller',
        'menu_position'      => 5,
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
        'taxonomies'       => array('result','category', 'post_tag')

    );

    register_post_type( 'result', $args );
}

REST API를 사용하여 WordPress의 사용자 지정 게시물 유형에서 데이터를 반환하는 방법입니다.

http://ejobbox.com/wp-json/wp/v2/customposttype이렇게 하면 사용자 지정 게시 유형 항목의 처음 10개 결과가 반환됩니다.

Custom Post type UI 플러그인을 사용하는 사람이 있다면, Post type 설정으로 들어가 UI를 통해 해야 합니다.

enter image description here

당신이 잘못한 것:

두가지 기능이 있습니다.codexres_custom_init그리고.codex_result_init그리고 두 기능 모두 불필요한 동일한 포스트 타입을 등록합니다.비록 두번째 기능을 위해codex_result_init당신은 그것을 추가하지 않았습니다.add_action('init','function_name'). 그래서 당신의 경우에는 그 기능이codexres_custom_init필요하지 않습니다.사용자 지정 게시 유형 작성에 대한 자세한 내용은 이 문서를 참조하십시오.

사용자 지정 게시 유형에 대한 휴식 API

이것을 시도해 보세요, 제가 볼 수 있는 것은 결과 포스트 타입을 두 번 등록한다는 것입니다.비록 당신이 본론에 참여하지는 않았지만요.

function codex_result_init() {
$labels = array(
    'name'               => _x( 'Result', 'post type general name', 'your-plugin-textdomain' ),
    'singular_name'      => _x( 'Result', 'post type singular name', 'your-plugin-textdomain' ),
    'menu_name'          => _x( 'Result', 'admin menu', 'your-plugin-textdomain' ),
    'name_admin_bar'     => _x( 'Result', 'add new on admin bar', 'your-plugin-textdomain' ),
    'add_new'            => _x( 'Add New', 'Result', 'your-plugin-textdomain' ),
    'add_new_item'       => __( 'Add New Result', 'your-plugin-textdomain' ),
    'new_item'           => __( 'New Result', 'your-plugin-textdomain' ),

    'edit_item'          => __( 'Edit Result', 'your-plugin-textdomain' ),

    'view_item'          => __( 'View Result', 'your-plugin-textdomain' ),

    'all_items'          => __( 'All Result', 'your-plugin-textdomain' ),

    'search_items'       => __( 'Search Result', 'your-plugin-textdomain' ),

    'parent_item_colon'  => __( 'Parent Result:', 'your-plugin-textdomain' ),
    'not_found'          => __( 'No Result found.', 'your-plugin-textdomain' ),
    'not_found_in_trash' => __( 'No Result found in Trash.', 'your-plugin-textdomain' )
);

$args = array(
    'labels'             => $labels,
    'description'        => __( 'Description.', 'your-plugin-textdomain' ),
    'public'             => true,
    'publicly_queryable' => true,
    'show_ui'            => true,
    'show_in_menu'       => true,
    'show_in_rest'       => true,
    'query_var'          => true,
    'menu_icon'          => 'dashicons-admin-users',
    'rewrite' => array( 'slug' => __('result', 'result')),
    'capability_type'    => 'post',
    'has_archive'        => true,
    'hierarchical'       => false,
    'rest_base'          => 'result',
    'rest_controller_class' => 'WP_REST_Posts_Controller',
    'menu_position'      => 5,
    'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
    'taxonomies'       => array('result','category', 'post_tag')

);

register_post_type( 'result', $args );
}
  add_action( 'init', 'codex_result_init' );

아래 코드만 제거하면 됩니다.

function codexres_custom_init() {
    $args = array(
      'public' => true,
      'label'  => 'Result'
    );
    register_post_type( 'result', $args );
}
add_action( 'init', 'codexres_custom_init' );

당신은 이 일에 관여하지 마세요.

이것을 사용합니다.

//add this to your functions.php file in your theme folder
function sb_add_cpts_to_api( $args, $post_type ) {
if ( 'result' === $post_type ) {
$args['show_in_rest'] = true;
}
return $args;
}
add_filter( 'register_post_type_args', 'sb_add_cpts_to_api', 10, 2 );

는 여기서 그것을 받았습니다.

부두교처럼 효과가 있었습니다.

언급URL : https://stackoverflow.com/questions/48536646/how-can-i-get-data-from-custom-post-type-using-wp-rest-api