获取自定义POST类型REST API不起作用

时间:2019-11-22 作者:sialfa

我想使用wordpress的RESTAPI加载模态的内容。我添加了show_in_rest 参数设置为我在函数文件中注册的自定义帖子类型,但我无法加载它,我将收到此错误rest_no_route. 我怎样才能解决这个问题?

功能:

function staff()
  {
    $labels = array(
      \'name\'               => _x( \'Team\', \'post type general name\'),
      \'singular_name\'      => _x( \'Team\', \'post type singular name\'),
      \'menu_name\'          => _x( \'Team\', \'admin menu\'),
      \'name_admin_bar\'     => _x( \'Team\', \'add new on admin bar\'),
      \'add_new\'            => _x( \'Nuova Persona\', \'Aggiungi membro team\'),
      \'add_new_item\'       => __( \'Name\'),
      \'new_item\'           => __( \'Aggiungi Persona\'),
      \'edit_item\'          => __( \'Modifica Persona\'),
      \'view_item\'          => __( \'Visualizza Team\'),
      \'all_items\'          => __( \'Visualizza Tutti\'),
      \'featured_image\'     => __( \'Featured Image\', \'text_domain\' ),
      \'search_items\'       => __( \'Cerca Persona\'),
      \'parent_item_colon\'  => __( \'Parent:\'),
      \'not_found\'          => __( \'No member found.\'),
      \'not_found_in_trash\' => __( \'No member found in Trash.\'),
    );

    $args = array(
      \'labels\'             => $labels,
      #\'menu_icon\'       => \'dashicons-star-half\',
      \'description\'        => __( \'Description.\'),
      \'public\'             => true,
      \'publicly_queryable\' => true,
      \'show_ui\'            => true,
      \'show_in_menu\'       => true,
      \'show_in_rest\'       => true,
      \'query_var\'          => true,
      \'rewrite\'            => true,
      \'capability_type\'    => \'post\',
      \'has_archive\'        => true,
      \'hierarchical\'       => true,
      \'menu_position\'      => null,
      \'supports\'           => array(\'title\',\'editor\',\'thumbnail\',\'custom-fields\')
    );

    register_post_type( \'staff\', $args );
  }
  add_action(\'init\', \'staff\');
JS/AJAX代码:

  $(\'.staff-link\').on(\'click\', function(e){
    e.preventDefault();
    var id = $(this).attr(\'data-id\');
    var type = $(this).attr(\'data-type\');
    console.log(id);
    $.getJSON(\'https://localhost/wordpress/wp-json/wp/v2/\'+type+\'&id=\'+id, function(data){
      console.log(data);
    });
  });

1 个回复
SO网友:Sam

对于那些想忘记WordPress REST API的人,我有另一个解决方案。

创建自己的PHP文件,在其中返回所需数据的JSON代码。文件的基本结构:

(注意:需要通过将文件包含在functions.php和add\\u操作“init”中来初始化文件)

<?php
$autorisation = isset($_POST[\'activate\']) ? $_POST[\'activate\'] : \'\';
$id = isset($_POST[\'id\']) ? $_POST[\'id\'] : \'\';

if($autorisation !== \'call-staff-post\'){
    return;
}

if(empty($id)){
    // If id empty, return all data
    $args = array(\'post_type\' => \'staff\', \'posts_per_page\' => \'-1\');
} else {
    $args = array(\'post_type\' => \'staff\', \'p\' => $id);
}

$staff = new WP_Query($args);

$return = array();
while($staff->have_posts()) : $staff->the_post();
    $return[] = array(
        \'title\' => get_the_title(),
    );
endwhile;

echo json_encode($return);

exit;
?>
您的JS/POST电话:

$.post(window.location.href, {
    activate: \'call-staff-post\',
    id: YOUR ID // Need to be integer not a string
}, function(data){
    var json = JSON.parse(data);

    console.log(json);
});