【问题标题】:JSON using wp_Query in wordpress?在wordpress中使用wp_Query的JSON?
【发布时间】:2014-09-05 04:58:56
【问题描述】:

我正在 wordpress 中使用 wp_Query 尝试 JSON,但我收到错误 Fatal error: Cannot use object of type WP_Post as array。 这是我的代码

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
require('wp-config.php');

//contoh parameter post per kategori >> category_name=kesehatan
//contoh parameter post per kategori >> showposts=5


$posts = array();
$the_query = new WP_Query( 'showposts=5' ); 

while ($the_query -> have_posts()) : $the_query -> the_post(); 

$thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail_name');
    $post[thumbnail_post]= $thumb[0]; // Fatal error: Cannot use object of type WP_Post as array
    $post[waktu_post]= get_the_time('Y-m-d H:i');// Fatal error: Cannot use object of type WP_Post as array
    $post[link_detail_post]= the_permalink();// Fatal error: Cannot use object of type WP_Post as array
    $post[judul_post]= title(); // Fatal error: Cannot use object of type WP_Post as array
    $post[ringkasan_post]= the_excerpt(__('(more…)'));// Fatal error: Cannot use object of type WP_Post as array

    $posts[] = $post;
endwhile;

echo json_encode(array('post'=>$posts));

?>

如何解决? 对不起我的英语

【问题讨论】:

    标签: php json wordpress


    【解决方案1】:

    $post 可能是一个全局变量,包含来自当前帖子的数据,因此将变量从$post 更改为$myData 之类的其他变量,它应该可以正常工作。 参考$post

    【讨论】:

      【解决方案2】:

      尝试重命名 $post 变量。可能引用了 GLOBAL $post 变量,这就是您收到错误的原因。

      【讨论】:

        猜你喜欢
        • 2011-10-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        • 2013-09-26
        • 2019-08-29
        • 1970-01-01
        • 1970-01-01
        • 2022-11-26
        相关资源
        最近更新 更多