如何使用Wordpress搭建微信小程序(一)获取分页文章api
2019-01-11 11:43
597 查看
我自己写了一个Wordpress小程序,总结了一些经验在这里分享给大家。
我们想要让小程序具有实时的数据,首先第一步先要得到接口,来获取我们想要的数据。
这篇文章我们先来获取我们自定义文章类型的分页文章数据。
Wordpress自带的api查询查询出来的结果,不是我们想要的,例如https://www.pipipi.net/wp-json/wp/v2/posts/
显示的结果
默认的显示所有posts存在一个问题,即获取不到我们自定义的post type。
因此我们需要自定一个路由api,来获取我们自己想要的数据。
[code] //posts register_rest_route('pipipi/v1','posts',array( 'methods'=>'GET', 'callback'=>function($data){ $postsResult = array( 'postInfo'=>array(), 'postsCount'=>null ); //postInfo返回所有的文章数据 //postsCount返回查询到的所有文章,用来判断是否已经查询完毕 $offset = $data['offset']; if(!$offset){ $offset = 0; } $args = array( 'post_status'=>'publish', 'post_type'=> array('diy', 'travel','code','cook','post'), 'orderby'=>'date', 'order'=>'DESC', 'posts_per_page'=>6, 'offset' => $offset ); $args2 = array( 'post_status'=>'publish', 'post_type'=> array('diy', 'travel','code','cook','post'), 'nopaging'=>true ); $query = new WP_Query($args); $query2 = new WP_Query($args2); while($query->have_posts()){ $query->the_post(); $post_id=get_the_ID(); $content = apply_filters('the_content', get_post_field('post_content', $post_id)); $like_count = get_post_meta(get_the_ID(),'suxing_ding',true); if(!$like_count){ $like_count= '0'; } $post_format = get_post_format($post_id); preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER); $attachment_image = wp_get_attachment_image_src( get_post_thumbnail_id($post_id), 'full')[0]; if($strResult[1]){ $images = array_slice($strResult[1],0,3); }else{ $images = null; } $post_format_array = array( 'format'=>array(), 'info'=>'' ); $post_format_array['format']=$post_format; if($post_format=='image'){ $post_format_array['info']='多图文章'; }elseif($post_format=='gallery'){ $post_format_array['info']='左图文章'; }else{ $post_format_array['info']='无图文章'; } $date = timeago( get_gmt_from_date(get_the_time('Y-m-d G:i:s')) ); array_push($postsResult['postInfo'],array( 'title'=>get_the_title(), 'category_names'=>get_the_category(), 'post_format'=>get_post_format($post_id), 'author'=>get_the_author(), 'views'=>get_post_meta($post_id, 'views', true), 'comments_count'=>get_comments_number($post_id), 'like_count'=>$like_count, 'post_format'=>$post_format_array, 'images'=>$images, 'atachment-image'=> $attachment_image, 'default-image'=>constant("THUMB_SMALL_DEFAULT"), 'excerpt'=>get_the_excerpt(), 'date'=>$date, 'post_id'=>$post_id )); } $postsResult['postsCount']=$query2->post_count; return $postsResult; } ));
这里返回的每一个数组项,都可以定制我们想要的数据。
我这里返回了文章显示类型,特色图,还有文章前三章图片。
offset字段用来判断当前是第几页。
相关文章推荐
- 微信小程序使用百度api获取天气信息 —— 微信小程序教程系列(16)
- 拿到微信小程序APPID了如何使用https版API中心来开发
- 微信小程序使用百度api获取天气信息 —— 微信小程序教程系列
- WordPress JSON REST API (WP API) 使用文档-创建\获取\编辑\删除文章
- 拿到微信小程序APPID了如何使用https版API中心来开发
- vb6如何设置和获取 cookie,使用 WinInet api url
- wordpress如何获取文章图片及图片路径
- [转]微信小程序联盟 跳坑《一百八十一》设置API:wx.openSetting使用说明
- 黄聪:如何不用插件使用代码调用Wordpress中的随机文章。
- Delphi的TService的輸入桌面切換(服务程序)(windows登录界面如何截图)(使用了OpenDesktop和GetThreadDesktop等API)
- 如何用Baas快速在腾讯云上开发小程序-系列1:搭建API & WEB WebSocket 服务器
- wordpress文章分页如何为标题添加页码
- 黄聪:如何使用WordPress 2.9内置文章缩略图功能(Post Thumbnail)
- PHPCMS使用get标签获取文章列表过滤重复标题并实现分页
- 微信小程序 获取设备信息 API实例详解
- 个人如何获取微信小程序的APPID
- 微信小程序开发之从相册获取图片 使用相机拍照 本地图片上传
- 让Wordpress成为你微信小程序的文章管理利器
- 如何获取错误消息说明使用 FormatMessage API
- 微信小程序的如何使用全局属性 —— 微信小程序教程系列(5)