slug 似乎不支持斜线(slug '/test/slug' 变成
进入“/testslug”)。
是的,因为无论您使用 REST API 还是管理 UI 来创建页面,slug 都仅限于 字母数字 字符,加上破折号 (-) 和下划线 (_)。见https://developer.wordpress.org/rest-api/reference/pages/#schema-slug。
您可以通过将remove_filter( 'sanitize_title', 'sanitize_title_with_dashes', 10, 3 ); 添加到主题的functions.php 文件(或自定义插件)来消除限制;但是,默认情况下访问页面 (URL) 会引发 404(未找到)错误。有一些方法可以解决这个问题,但简而言之,您不应该删除 filter。
是否可以使用rest api在自定义url上创建页面?
不,不是,默认情况下不是。
但是,如果您希望 http://example.com/test/slug 提供/显示页面/帖子/等。无论是否通过 REST API 创建,您都可以使用自定义 URL 重写规则,例如通过add_rewrite_rule()。
创建的页面在
标题。如何正确创建自定义元标记?
您需要注册元 key,在您的情况下是 description。
见
您可以使用 wp-includes/meta.php 中的register_meta() 函数注册它。
description 元的示例:
<?php
// The object type. For custom post types, this is 'post';
// for custom comment types, this is 'comment'. For user meta,
// this is 'user'.
$object_type = 'post'; // 'post' even for Pages
$args1 = array( // Validate and sanitize the meta value.
// Note: currently (4.7) one of 'string', 'boolean', 'integer',
// 'number' must be used as 'type'. The default is 'string'.
'type' => 'string',
// Shown in the schema for the meta key.
'description' => 'A meta key associated with a string meta value.',
// Return a single value of the type.
'single' => true,
// Show in the WP REST API response. Default: false.
'show_in_rest' => true,
);
register_meta( $object_type, 'description', $args1 );
要快速测试元 description 是否已成功注册以及是否可从 REST API 获得,请向 http://example.com/wp-json/wp/v2/pages/<id> 执行 GET 请求,其中 <id> 是页面 ID。