使用 Wordpress REST API 通过 url 获取页面

Ben*_*son 5 api rest wordpress routes angularjs

我正在尝试在使用漂亮 URL 而不是 ID(这在 REST 场景中很典型)的站点上使用 WP REST API。我所要查询的只是请求的 URL。

假设用户登陆example.com/about,我可以执行以下操作:

/wp-json/wp/v2/pages/?slug=about
Run Code Online (Sandbox Code Playgroud)

没有汗水。但是,如果用户登陆了example.com/about/team怎么办?我可以通过 slug 进行查询team,但也许我的网站也有example.com/contact/team可能的 URL。这不可靠。

我需要的是一种实现以下目标的方法:

/wp-json/wp/v2/pages/?permalink=about%2team
Run Code Online (Sandbox Code Playgroud)

必须为每个创建的页面设置自定义路由是不可行的。我很惊讶 API 似乎没有开箱即用的某种永久链接处理程序。

Cod*_*ist 1

好吧,由于没有工具可以处理所有情况,因此您必须有点狡猾。可以使用 wp 函数(如url_to_postidor )get_term_by和全局变量(如)的组合$wp_taxonomies来找出所有情况。

您可以创建自定义端点并使用下面的代码来处理路径。我认为我已经涵盖了所有可能的情况,但请小心使用,因为我可能会错过一些东西。

function ca_path_to_object($path = '/') {
    $path = $path === '/' ? $path : trim($path, '/');
    $path = $path === '' ? '/' : $path; // Return homepage for no arguments
    $for_front = intval(get_option('page_on_front'));
    $for_posts = intval(get_option( 'page_for_posts' ));
    if($path == '/') $pid = $for_front; // It is the homepage
    else $pid = url_to_postid($path); // It is not
    if($for_posts != 0) { //For some reason url_to_postid does not work if the page is set to be the blog feed
        if($for_posts == $for_front) { // if both $for_front & $for_posts set to the same page get_permalink returns 404 for some reason, so lets construct it manually
            $pst = get_post($for_posts);
            $prnt = intval($pst->post_parent);
            $url = '';
            if($prnt != 0) {
                $url = parse_url(get_permalink($prnt));
                $url = trim($url['path'], '/');
            }
            $url .= '/'.$pst->post_name;
            // if it matches it will return the results as Homepage as per default WP behaviour
        }
        else {
            $url = parse_url(get_permalink($for_posts)); // otherwise just get it
            $url = $url['path'];
        }
        if(trim($url, '/') == $path) $pid = $for_posts;
    }
    if($pid == 0) { // not a page or cpt, let's try to find a taxonomy out of it
        $path_arr = explode('/', $path);
        global $wp_taxonomies;
        $tax = null;
        foreach ($wp_taxonomies as $key => $value) {
            if ($value->rewrite['slug'] === $path_arr[0]){
                $tax = $key;
            }
        }
        if($tax !== null) {
            $term = get_term_by('slug', end($path_arr), $tax);
            if($term !== false) {
                $pid = $term->term_id;
                return array(
                    'type' => 'term',
                    'id' => $term->term_id,
                    'object' => $term,
                );
            }
        }
    }
    if($pid == 0) { // If still 0, perhaps it's an user profile url?
        global $wp_rewrite;
        $base = explode('/', trim($wp_rewrite->get_author_permastruct(), '/'));
        $base = $base[0];
        if($path_arr[0] === $base) {
            $user = get_user_by('slug', end($path_arr));
            if($user !== false) {
                $pid = $user->ID;
                return array(
                    'type' => 'user',
                    'id' => $user->ID,
                    'object' => $user,
                );
            }
        }
    }
    if(($pid == 0) && ($path == '/')) { // If still 0, lets check if the page is the homepage without any associated page entity
        return array(
            'type' => 'home',
            'id' => null,
            'object' => null,
        );
    }
    if($pid == 0) { // If still 0, return nothing found or 404
        return array(
            'type' => '404',
            'id' => null,
            'object' => null,
        );
    }
    $post = get_post($pid);
    if($post->ID == $for_front) { // It's a page assigned to be a homepage
        return array(
            'type' => 'home',
            'id' => $post->ID,
            'object' => $post,
        );
    }
    if($post->ID == $for_posts) { // It's a page assigned to be a posts feed
        return array(
            'type' => 'feed',
            'id' => $post->ID,
            'object' => $post,
        );
    }
    return array( // It's just a post object
        'type' => 'post',
        'id' => $post->ID,
        'object' => $post,
    );
}
Run Code Online (Sandbox Code Playgroud)