【问题标题】:How to add WP API and JS featured image attachment in Laravel如何在 Laravel 中添加 WP API 和 JS 特色图片附件
【发布时间】:2019-10-07 02:19:14
【问题描述】:

我想从 wordpress rest api 获取图像 - 我有以下代码: HTML刀片:

<div class="post-item">
   <div id="posts">Loading posts...
</div>
  

Ajax 脚本:

<script type="text/javascript">
    $(document).ready(function () {
        $.ajax({
            type: 'GET',
            url: 'https://mysite.io/blog/wp-json/wp/v2/posts?_embed&per_page=3',            

            success: function (data) {
                var posts_html = '';
                $.each(data, function (index, post) {
                  
                  posts_html += '<div class="post-item-image">';
                  posts_html += '<a href="' + post.source_url + '"></a>';
                  posts_html += '<img src="' + + '"</div>';
                  posts_html += '<div class="post-item-header">';
                  posts_html += '<span class="date">' + post.date + '</span>';
                  posts_html += '<span class="user">';
                  posts_html += '<a href="' + post.link +'">';
                  posts_html += '<img src="https://mysite.io/images/users/mysite-1548344709.jpg">Mysites</a></span></div>';
                  posts_html += '<div class="post-item-body">';
                  posts_html += '<a href="' + post.link + '" style="text-decoration: underline;"> '+ post.title.rendered + '</a>';
                  posts_html += '<div class="post-short-text"> ' + post.excerpt.rendered + '</div></div>';
                });
                $('#posts').html(posts_html);
            },
            error: function (request, status, error) {
                alert(error);
            }
        });
    });
</script>   

问题在于 wp:featuredmedia... 这一行:

posts_html += '<img src="' +  + '"</div>';

我试过也试过,但到目前为止没有运气

posts_html += '<img src="' + post.embed["wp:featuredmedia"][0].media_details.sizes.full.source_url + '"</div>';

有什么建议吗?

【问题讨论】:

    标签: jquery ajax laravel wordpress-rest-api wp-api


    【解决方案1】:

    我设法通过将此代码(在底部)添加到您的博客主题我的路径来解决它:wp-content/themes/twentysixteen/function.php:

    function ws_register_images_field() {
        register_rest_field( 
            'post',
            'images',
            array(
                'get_callback'    => 'ws_get_images_urls',
                'update_callback' => null,
                'schema'          => null,
            )
        );
    }
    
    add_action( 'rest_api_init', 'ws_register_images_field' );
    
    function ws_get_images_urls( $object, $field_name, $request ) {
        $medium = wp_get_attachment_image_src( get_post_thumbnail_id( $object->id ), 'medium' );
        $medium_url = $medium['0'];
    
        $large = wp_get_attachment_image_src( get_post_thumbnail_id( $object->id ), 'large' );
        $large_url = $large['0'];
    
        return array(
            'medium' => $medium_url,
            'large'  => $large_url,
        );
    }

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-10-29
      • 2013-08-31
      • 1970-01-01
      • 2016-09-26
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多