0

I'm having trouble to get some custom post type using the REST API. I get a 404 message but all the settings are right. Is there a fix?

<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">&times;</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>

The post type is set to public and has the show in rest attribubte.

0

You need to declare that your CPT supports REST via show_in_rest when registering it, then use the REST API endpoint for that post type.

For example, I have a CPT on my own site for talks named tomjn_talks, and it has an endpoint at /wp/v2/tomjn_talks

|improve this answer|||||
  • 1
    yeah, that's truth. I've solved – sialfa Feb 25 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.