如何在Wordpress REST API中检索类别/标记列表

时间:2013-11-23 04:07:36

标签: wordpress

有谁知道如何在Wordpress JSON Rest API中获取类别列表? 看来当前的API不支持检索类别列表(虽然XML-RPC确实如此)。

http://developer.wordpress.com/docs/api/

7 个答案:

答案 0 :(得分:4)

我正试图从我的wordpress网站制作andriod app。感谢JSON API可能,但真正的头痛。请求语法没有任何文档。

经过2个小时的研究,我终于挖出了一些东西。首先要知道的是,有三种类型的请求模式:

<强> 1。隐含模式

JSON查询使用非空值,即“json = 1”。例如:

<强> 2。明确模式

JSON查询使用已知的字符串值,即“json = get_recent_post”。示例:

第3。永久链接模式

没有JSON查询,但用户友好的永久链接用于请求,即“/api/get_recent_post".Examples:

此外,列出类别的语法是:

<强> http://blog.example.com/?json=get_category_index

其他重要的基本要求是:

  1. http://blog.example.com/?json=get_tag_index (获取代码列表)
  2. http://blog.example.com/?json=get_author_index (获取作者列表)
  3. http://blog.example.com/?json=get_page_index (获取页面列表)
  4. http://blog.example.com/?json=get_date_index (获取日期列表)
  5. 可在此link中找到更多详细信息。希望这能节省一些不像我这样的wordpress背景的人的时间。

答案 1 :(得分:2)

WP REST API页,您可以点击http://example.com/wp-json/wp/v2/categories。这可能是Rest API v2的补充,但我不确定。

答案 2 :(得分:0)

试试这个<?php echo get_the_category_list(); ?>

答案 3 :(得分:0)

如果你使用Jetpack提供的JSON API,你可能会失败。查看他们的API文档(他们声称自动更新),没有参考获取类别列表。但是,如果它有帮助,您可以获取有关每个类别的信息。

我刚试过http://public-api.wordpress.com/rest/v1/sites/ $ site / categories而且没有返回任何内容。遗憾。

http://developer.wordpress.com/docs/api/

答案 4 :(得分:0)

最终可行的步骤:

  1. 安装wordpress插件:JSON API wordpress plugin JSON API
  2. 使用get_category_index api,格式为:http://www.example.com/api/get_category_index
  3. 然后可以获得响应类别:

    {
        "status": "ok",
        "count": 332,
        "categories": [{
            "id": 4637,
            "slug": "soft_360",
            "title": "360",
            "description": "",
            "parent": 4618,
            "post_count": 2
        }, {
            "id": 4498,
            "slug": "amazon",
            "title": "Amazon",
            "description": "",
            "parent": 3390,
            "post_count": 29
        }, {
        ......
        }, {
            "id": 860,
            "slug": "default_classification",
            "title": "\u9ed8\u8ba4\u5206\u7c7b",
            "description": "",
            "parent": 17,
            "post_count": 3
        }]
    }
    

    更多信息可参考官方文件: JSON API — WordPress Plugins

答案 5 :(得分:0)

$args = [
    'taxonomy' => 'category',
    'hide_empty' => 0,
    'parent' => 0
];

function _get_child_terms( $items ) {
    foreach ( $items as $item ) {
      $item->children = get_terms( 'category', array( 'child_of' => $item->term_id, 'hide_empty' => 0 ) );
      if ( $item->children ) _get_child_terms( $item->children );
    }
    return $items;
}

$terms = _get_child_terms( get_terms( $args ) );
echo json_encode( $terms );

答案 6 :(得分:0)

在当前版本的Wordpress 4.9.8中,通常可以通过内置API通过以下方式获取类别:

http://www.example.com/wp-json/wp/v2/categories

但是,似乎存在一个无法获取所有类别的错误,至少在使用父类别和子类别的情况下。

我在我们的wordpress安装中添加了一个小的PHP脚本,以正确检索所有类别:

<?php

/** Make sure that the WordPress bootstrap has run before continuing. */
require( dirname(__FILE__) . '/wp-load.php' );

// Redirect to https login if forced to use SSL
if ( force_ssl_admin() && ! is_ssl() ) {
    if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
        wp_safe_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
        exit();
    } else {
        wp_safe_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
        exit();
    }
}

header('Content-Type: application/json');
echo json_encode(get_categories());

注意:

出于测试目的,您应该知道get_categories函数将仅返回已经与至少一篇(已发表的)文章相关联的函数。