WordPress JSON output

时间:2012-11-13 作者:PHPLearner

更新:我更改了代码如下

$cats = get_categories();
$output = array(\'categories\' => array());

function collect_posts(){
    $args = array( \'numberposts=\' => 9999, \'category\' => $cat );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) {
        setup_postdata($post); 
        if (get_post_meta($post->ID, \'apls_video_youtube\', true) != \'\'){
        $url = \'http://www.youtube.com/watch?v=\';
        $url .= get_post_meta($post->ID, \'apls_video_youtube\', true);
        } else {
            $url = get_post_meta($post->ID, \'apls_video_hosted\', true);
        }
        $output[\'posts\'][] = array(
            \'name\' => get_the_title($post->ID),
            \'url\' => $url,
        );
        return $output;
    }       
}

foreach ($cats as $cat) {

    $output[\'categories\'][] = array(
        \'cat_id\' => $cat->term_id,
        \'cat_name\' => $cat->name,
        collect_posts()
    );
}

header("Content-type: application/json");
die(json_encode($output));
输出为:

{
    "categories": [
        {
            "cat_id": "555",
            "cat_name": "Articles",
            "0": {
                "posts": [
                    {
                        "name": "title",
                        "url": "http://www.domain.com......."
                    }
                ]
            }
        },
        {
            "cat_id": "15",
            "cat_name": "Crank",
            "0": {
                "posts": [
                    {
                        "name": "title examlple",
                        "url": "http://www.domain.com/v......"
                    }
                ]
            }
        },
        {......
问题是:

仅显示一篇文章在每个类别中显示同一篇文章添加了0(零)

所以我需要你的建议来解决这个问题。

============================上一部分=========================

我想得到json输出

 {
    "categories": [
        {
            "cat_id": "16",
            "cat_name": "Arm Lock",
            "posts": [
                {
                    "name": "Video1",
                    "url": "http://www.videourl1.com/"
                },
                {
                    "name": "Video2",
                    "url": "http://www.videourl2.com/"
                },
                {
                    "name": "Video2",
                    "url": "http://www.videourl3.com/"
                }
            ]
        },
        {
            "cat_id": "12",
            "cat_name": "Kick",
            "posts": [
                {
                    "name": "Video1",
                    "url": "http://www.videourl3.com/"
                },
                {
                    "name": "Video2",
                    "url": "http://www.videourl3.com/"
                },
                {
                    "name": "Video2",
                    "url": "http://www.videourl3.com/"
                }
            ]
        }
    ]
}
编码为

// get all the categories from the database
$cats = get_categories();

header(\'Content-type: application/json\');
echo \'{
   "categories": [\';
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
query_posts("cat=$cat_id&posts_per_page=9999");
echo \'
      {
    "cat_id": "\'.$cat_id.\'",\';
echo \'
    "cat_name": "\'.$cat->name.\'",\';
echo \'
    "posts": [\';
if (have_posts()) : while (have_posts()) : the_post();
if (get_post_meta($post->ID, \'apls_video_youtube\', true) != \'\'){
    $url = \'http://www.youtube.com/watch?v=\';
    $url .= get_post_meta($post->ID, \'apls_video_youtube\', true);
} else {
    $url = get_post_meta($post->ID, \'apls_video_hosted\', true);
}

echo \'  
      {\';
echo \'  
        "name": "\'.get_the_title().\'", \';
echo \'  
        "url": "\'.$url.\'"\';
echo \'  
      },\';
endwhile; endif; 
echo \'
     ]\';
echo \'
   },\';
}

echo \'
  ]\';
echo \'
}\';
这对我来说很有效,但在每个循环的最后一项中都会遇到“,”的问题,所以JSON输出对事务长来说是无效的。我怎样才能修复它?

1 个回复
最合适的回答,由SO网友:totels 整理而成

如果您使用的是PHP 5.2+,那么最好的选择就是创建一个PHP数组或对象,并使用json_encode().

更新日期:

$cats = get_categories();
$output = array(\'categories\' => array());

foreach ($cats as $cat) {
    $cat_output = array(
        \'cat_id\' => $cat->term_id,
        \'cat_name\' => $cat->name,
        \'posts\' => array(),
    );

    // should be able to use -1 to get all posts, rather than 9999
    $args = array(\'numberposts=\' => -1, \'category\' => $cat->cat_ID);
    $myposts = get_posts($args);

    foreach( $myposts as $post ) {
        if ($youtube_id = get_post_meta($post->ID, \'apls_video_youtube\', TRUE)) {
            $url = "http://www.youtube.com/watch?v={$youtube_id}";
        } else {
            $url = get_post_meta($post->ID, \'apls_video_hosted\', TRUE);
        }

        $cat_output[\'posts\'][] = array(
            \'name\' => get_the_title($post->ID),
            \'url\' => $url,
        );
    }

    $output[\'categories\'][] = $cat_output;
}

header("Content-type: application/json");
die(json_encode($output));

结束

相关推荐

如何替换或显示JSON API插件中的特殊字符

我开始使用JSON API插件。它工作得很好!但我面临的问题是,如果我通过CMS输入任何包含特殊字符的文本,输出结果与给定的文本不一样。为什么会这样?示例:这是一个很好的应用程序(输入),输出看起来像';这是一个很好的应用程序。请提出任何摆脱这种情况的提示/方法。急切地等待你的回应。非常感谢。