【问题标题】:JSON WP REST API getting featured image by Taxonomy TermsJSON WP REST API 通过分类术语获取特色图像
【发布时间】:2019-01-03 02:37:11
【问题描述】:

我正在尝试从我的自定义 taxonomy-*projects* term-*elfla*. 中获取特色图片

我设法从帖子中使用我找到的 here 代码获得了一张图片,但我不明白如何使用分类术语。

此时我不明白如何获取特色图片源以及'GET'请求url是否正确。

JS:

var portfolioPostsBtn = document.getElementById("portfolio-posts-btn");
var portfolioPostsContainer = document.getElementById("portfolio-posts-container");

if (portfolioPostsBtn) {
  portfolioPostsBtn.addEventListener("click", function() {
    var ourRequest = new XMLHttpRequest();
    ourRequest.open('GET','http://localhost/test/wordpress/wp-json/wp/v2/posts/?filter=projects&filter=elfla&_embed'); 
    ourRequest.onload = function() {
      if (ourRequest.status >= 200 && ourRequest.status < 400) {
        var data = JSON.parse(ourRequest.responseText);
        createHTML(data);
        portfolioPostsBtn.remove();
        console.log(data);
      } else {
        console.log("We connected to the server, but it returned an error.");
      }
    };
    ourRequest.onerror = function() {
      console.log("Connection error");
    };

    ourRequest.send();
  });
}

function createHTML(postsData) {
  var ourHTMLString = '';
  for (i = 0; i < postsData.length; i++) {

   ourHTMLString += '<img src="'+postsData[i].featured_image_thumbnail_url+ '" alt="img">';

  }
  portfolioPostsContainer.innerHTML = ourHTMLString;




  
}

Functions.php

function my_rest_prepare_post( $data, $post, $request ) {
$_data = $data->data;
$thumbnail_id = get_term_meta( $post->ID , 'thumbnlai' false );
$thumbnail = wp_get_attachment_image_src( $thumbnail_id );
$_data['featured_image_thumbnail_url'] = $thumbnail[0];
$data->data = $_data;
return $data;
}
add_filter( 'rest_prepare_post', 'my_rest_prepare_post', 10, 3 );

【问题讨论】:

    标签: json wordpress api


    【解决方案1】:

    使用此功能。

    **function custom_api_get_all_posts() {
    register_rest_route( 'custom/v1', '/all-posts', array(
        'methods' => 'GET',
        'callback' => 'custom_api_get_all_posts_callback'
    )); } 
    function custom_api_get_all_posts_callback( $request ) {
    // Initialize the array that will receive the posts' data. 
    $posts_data = array();
    // Receive and set the page parameter from the $request for pagination purposes
    $paged = $request->get_param( 'page' );
    $paged = ( isset( $paged ) || ! ( empty( $paged ) ) ) ? $paged : 1; 
    // Get the posts using the 'post' and 'news' post types
    $posts = get_posts( array(
            'paged' => $paged,
            'post__not_in' => get_option( 'sticky_posts' ),
            'posts_per_page' => 1000,            
            'post_type' => array('post') // This is the line that allows to fetch multiple post types. 
        )
    ); 
    // Loop through the posts and push the desired data to the array we've initialized earlier in the form of an object
    foreach( $posts as $post ) {
        $id = $post->ID; 
        $post_thumbnail = ( has_post_thumbnail( $id ) ) ? get_the_post_thumbnail_url( $id ) : null;
    
        $posts_data[] = (object) array( 
            'id' => $id, 
            'slug' => $post->post_name, 
            'type' => $post->post_type,
            'title' => $post->post_title,
            "content" => apply_filters('the_content', $post->post_content),
            'featured_img_src' => $post_thumbnail
        );
    }                  
    return $posts_data; }
    

    使用此链接http://yourdomin.com/wp-json/custom/v1/all-posts。 我认为它会为你工作

    【讨论】:

    • 你能解释一下如何在我的 javascript 文件中使用它吗?
    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多