Understanding WPREST API Output

Asked 2 years ago, Updated 2 years ago, 49 views

We are currently working on WordPress 4.7.5.
In order to reduce the weight of the site, we are currently updating it to print article information using WP REST APIv2, but we are investigating the following points on Google, but we are having a hard time because we did not get the article.

·Custom Post Type Category Output
·Advanced Custom Field Pro creation items json shaping and html display

I would appreciate it if you could tell me about these two points.
The ACF to REST API is implemented and the output is verified at http://example.com/wp-json/wp/v2/example(acf).
Regarding the custom post type category, I have confirmed that the ID has been output, but I have not been able to confirm the output of the registered name and I have not been able to shape it.

The following is a description of calling, shaping, and output:

$(function(){
  $html_set=';
  $.getJSON("http://aquariumbynaked.com/wp-json/wp/v2/latest-contents/?_embed", function(data){

    $html_set='<style>'+
      'ul.wp_api-unit {display:block;}' +
      'ul.wp_api-unit li { clear: both; margin-bottom: 15px; padding-bottom: 15px; border-bottom:1px dotted #cccccc;}' +
      'ul.wp_api-unita {width: 100%; display:block;}' +
      'ul.wp_api-unit.thumbnail {display:inline-block; width:28%; margin:0; padding:02%00; vertical-align:top;}' +
      'ul.wp_api-unit.contents {display:inline-block; width:70%;}' +
      'ul.wp_api-unit.contents h1 { font-size: 1.6em; margin:0; paddin-bottom:15px; margin:0; padding:0;}' +
      'ul.wp_api-unit.contentsp {text-align:right; width:100%;}' +
      '</style>';

    $html_set+='<div>';

   $(data).each(function(){

    $title=this.single_title;
    $excerpt=this.excerpt;
    $date = new Date(this.date_gmt);
    $month = $date.getMonth()+1;
    $date_set = $date.getFullYear() + '.' + $month + '.' + $date.getDate() + '.';
    $thumbnail_src = this._embedded ["wp:featureedmedia" ][0].media_details.sizes.full.source_url;

    $html_set += '<div class="contents-inner box-area pos-r">' + '<div class="contents-image thumbnail-55 thumbnailImg pos-r" style="background-image: url('+ $thumbnail_src +')"></div>' + '<a href="' + this.link + '" target="_blank" class="contents-links web-links pos-a-area"></a>' + '<div class="contents-text pos-a">' + '<h3>' + $title +'</h3>'+'<div class "event-date">'+$date+'</div>'+'</div>'+

    '</div>'+
    '';
    })
    $html_set+='</div>';
    $($html_set).appendTo('.LatestContents');
  })
});

I'm sorry for my lack of knowledge. Thank you for your cooperation.

json wordpress api rest

2022-09-29 21:48

1 Answers

In order to display the script correctly, I wrote it in the answer field.
This is an example of using jquery to retrieve the term name specified in the post article (posts) when you created the taxonomi as "test_taxonomy".

$(function(){
    var taxonomys = new Object;
    $.getJSON('http://hoge.jp/wp-json/wp/v2/test_taxonomy', function(json){
        // test_taxonomy term ID, creating translation table from term name
        for (vari=0;i<json.length;i++){
            taxonomys [json[i].id] = json[i].name;
        }

        // Convert test_taxonomy term ID to term name in article data
        $.getJSON('http://hoge.jp/wp-json/wp/v2/posts', function(json1){
            for (vari=0;i<json1.length;i++){
                for(varj=0;j<json1[i].test_taxonomy.length;j++){
                    varterm_name = taxonomies [json1[i].test_taxonomy[j]];
                    console.log(term_name);
                }
            }
        });
    });
});


2022-09-29 21:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.