我很难使用REST API获取一些自定义的post类型。我收到404信息,但所有设置都正确。有解决方案吗?
<div class="row services-content">
<?php $services = new WP_Query( [\'post_type\' => \'servizi\'] ); ?>
<?php if( wp_is_mobile() ): ?>
<div class="col-sm-12 text-center" style="padding:0;">
<div class="swiper-container services-carousel">
<div class="swiper-wrapper">
<?php if( $services->have_posts() ): while( $services->have_posts() ): $services->the_post(); ?>
<div class="swiper-slide" style="background-image:url(\'<?php echo the_post_thumbnail_url(); ?>\');">
<h4 class="service-slide-name"><?php the_title(); ?></h4>
</div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
</div>
<!-- Pagination -->
<div class="swiper-pagination"></div>
<!-- Arrows -->
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div>
</div>
<?php else: ?>
<?php if( $services->have_posts() ): while( $services->have_posts() ): $services->the_post(); ?>
<div class="d-sm-none d-md-block col-md-3 col-lg-3 service-col">
<img class="rounded-circle service-icon hide" src="<?php echo the_post_thumbnail_url(); ?>" width="60">
<a class="open-modal" href="#modal-content" data-id="<?php echo $post->ID; ?>" data-toggle="modal" style="text-decoration:none;color:black;"><h4><?php the_title(); ?></h4></a>
</div>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
<?php endif; ?>
</div>
<div class="modal" tabindex="-1" role="dialog" id="modal-content">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p class="content"></p>
</div>
</div>
</div>
</div>
<script type="text/javascript">
(function($){
$(document).ready(function(){
$(document).on(\'click\', \'.open-modal\', function(e){
e.preventDefault();
var id = $(this).attr(\'data-id\');
$.getJSON(\'wp-json/wp/v2/posts/\'+id , function(data){
console.log(data);
});
});
});
}(jQuery));
</script>
帖子类型设置为public,并在rest属性中显示。
最合适的回答,由SO网友:Tom J Nowell 整理而成
您需要通过以下方式声明您的CPT支持RESTshow_in_rest
注册时,请将REST API端点用于该post类型。
例如,我在自己的网站上有一个名为tomjn_talks
, 它的端点位于/wp/v2/tomjn_talks