加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

PHP 使用json格式post到wordpress的简单示例

发布时间:2020-05-30 21:32:29 所属栏目:PHP 来源:互联网
导读:PHP 使用json格式post到wordpress的简单示例

感兴趣的小伙伴,下面一起跟随脚本之家 jb51.cc的小编来看看吧。
经测试代码如下:


<?php
/**
 * 使用json格式post到wordpress
 *
 * @param 
 * @arrange (512.笔记) jb51.cc
 **/
// include our wordpress functions
// change relative path to find your WP dir
define('WP_USE_THEMES',false);
require('./wp-blog-header.php');
 
// set header for json mime type
header('Content-type: application/json;');
 
// get latest single post
// exclude a category (#5)
query_posts(array(
  'posts_per_page' => 5,'cat' => -5,));
 
$jsonpost = array();
if (have_posts()) {

 if ( have_posts() ) : while ( have_posts() ) : the_post();
 
  // construct our array for json
  // apply_filters to content to process shortcodes,etc
  $jsonpost["id"] = get_the_ID();
  $jsonpost["title"] = get_the_title();
  $jsonpost["url"] = apply_filters('the_permalink',get_permalink());
  // $jsonpost["content"] = apply_filters('the_content',get_the_content());

  $images = get_children( array( 'post_parent' => $post->ID,'post_type' => 'attachment','post_mime_type' => 'image','orderby' => 'menu_order','order' => 'ASC','numberposts' => 999 ) );
  if ( $images ) {
  $total_images = count( $images );
   $image = array_shift( $images );
   $jsonpost['featured_image'] = wp_get_attachment_image( $image->ID,'thumbnail' );
  }

  $jsonpost["content"] = get_the_content();

  // would rather do iso 8601,but not supported in gwt (yet)
  $jsonpost["date"] = get_the_time('d F Y');

  $jsonposts[] = $jsonpost;

 endwhile;

 endif;
 
} else {
  // deal with no posts returned
}
 
// output json to file

header('Cache-Control: no-cache,must-revalidate');
header('Expires: Mon,26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo json_encode($jsonposts);


/*** 来自:脚本之家 jb51.cc(jb51.cc) ***/ 
?>

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读