1、需要在@jianbo 的wp-rest-api-for-app插件里找到custom-fields-rest-prepare-post.php这个文件,打开之后找到$post_id =$post->ID;
这段代码,在下面添加下面这两段代码
$post_thumbnail = get_post_thumbnail($post_id);
$_data['thumbnail'] = $post_thumbnail;
2、需要在自己wordpress主题模板函数文件functions.php加上如下代码
function get_post_thumbnail($post_id){
$post = get_post($post_id);
$thumbnails = get_post_meta($post_id, 'thumbnail', true);
if(has_post_thumbnail()){
$post_thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post_id),'full');
return $post_thumbnail[0];
} else if (!empty($thumbnails)) {
$post_thumbnail = $thumbnails;
return $post_thumbnail;
} else {
$post_thumbnail = '';
ob_start();
ob_end_clean();
$post_images = preg_match('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$post_img_src = $matches[1];
if(!empty($post_img_src)){
$path_parts = pathinfo($post_img_src);
$first_img_name = $path_parts["basename"];
$expired = 604800;
$post_thumbnail = $post_img_src;
}
return $post_thumbnail;
}
}
3、需要在@jianbo开源的小程序,文章输出缩略图出,修改输出函数。
this.data.detail.thumbnail,
将上面的这代码跟imageUrl后边的代码替换,
onShareAppMessage: function (res) {
this.ShowHideMenu();
console.log(res);
return {
title:this.data.detail.title.rendered,
path: 'pages/detail/detail?id=' + this.data.detail.id,
imageUrl: this.data.thumbanil, //这是我的图片路径
success: function (res) {
// 转发成功
console.log(res);
},
fail: function (res) {
console.log(res);
好了,完成上述的步骤,你的文章分享后就会自动调取wordpress特色图片作为分享缩略图。
onShareAppMessage: function (res) {
this.ShowHideMenu();
console.log(res);
return {
title:this.data.detail.title.rendered,
path: 'pages/detail/detail?id=' + this.data.detail.id,
imageUrl:this.data.detail.thumbnail, //这是我的图片路径
success: function (res) {
// 转发成功
console.log(res);
},
fail: function (res) {
console.log(res);
这是修改过后的代码 感谢 jianbo chi 搞么罗的分享