我有一个自定义帖子类型,card该类型通过WP REST API公开。
function register_card_post_type() {
$labels = array(
"name" => __( 'Cards', '' ),
"singular_name" => __( 'Card', '' ),
);
$args = array(
"label" => __( 'Cards', '' ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true, // ADD TO REST API
"rest_base" => "cards", // ADD TO REST API
"has_archive" => false,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" …Run Code Online (Sandbox Code Playgroud) 我正在构建一个VBA应用程序,该应用程序使用从网络抓取的资源来创建和修改Wordpress网站页面。Wordpress API返回一个JSON文件,但是在VBA中不支持解析JSON,因此我从GitHub导入了VBA-JSON。这是子例程:
Sub Wordpress()
'
' Wordpress API Test
'
Dim wpResp As Variant
Dim sourceSheet As String
Dim resourceURL As String
sourceSheet = "Resources"
resourceURL = Sheets(sourceSheet).Cells(6, 1)
wpResp = getJSON(resourceURL + "/wp-json/wp/v2/posts")
End Sub
Run Code Online (Sandbox Code Playgroud)
和它调用的函数。
Function getJSON(link) As Object
Dim response As String
Dim json As Object
On Error GoTo recovery
Dim retryCount As Integer
retryCount = 0
Dim web As MSXML2.XMLHTTP60
Set web = New MSXML2.XMLHTTP60
the_start:
web.Open "GET", link, False, UserName, pw
web.setRequestHeader "Content-type", "application/json"
web.send …Run Code Online (Sandbox Code Playgroud) 我正在尝试从另一个WordPress网站获取博客文章,到目前为止,我已经成功获取了这些文章,并且正在使用以下代码段:
$response = wp_remote_get( add_query_arg( array(
'per_page' => 1,
'categories' => 38
), 'https://www.remotesite.com/wp-json/wp/v2/posts?_embed' )
);
if( !is_wp_error( $response ) && $response['response']['code'] == 200 ) {
$remote_posts = json_decode( $response['body'] );
foreach( $remote_posts as $remote_post ) {
echo '<h2>'. $remote_post->title->rendered . '</h2>
<p>' . $remote_post->excerpt->rendered . '</p>';
}
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我可以获取所有必需的详细信息,标题,摘录和特色图片。但是,我很难找到如何从上述响应中获取“特色图片”网址。谁能告诉我如何从响应中使用wp:featuredmedia。我在下面的代码中看到了获取特色图片URL的地方,但这对我没有帮助:
echo [your-data]._embedded['wp:featuredmedia']['0'].source_url
Run Code Online (Sandbox Code Playgroud) 我有一个设置自定义帖子类型的网站,用于定义主页号召性用语框。
标题、描述和特色图片都由编辑器的默认块/功能处理,但我正在尝试添加一个自定义块以将 url 保存到帖子的元数据中。
该块正确呈现,但未保存元数据,该updateBlockValue函数肯定会被调用。
我使用几乎相同的代码为页面和帖子创建自定义元块。这种方法是否不适用于自定义帖子类型?
这是我正在使用的代码:
PHP
function wb_blocks() {
wp_register_script(
'wb-blocks-js',
get_template_directory_uri() . '/scripts/block.js',
array( 'wp-blocks', 'wp-editor', 'wp-element','wp-components' )
);
register_block_type( 'ray/homebox-link-url', array(
'editor_script' => 'wb-blocks-js',
) );
}
add_action( 'init', 'wb_blocks' );
function wb_register_block_meta() {
register_meta( 'post', 'homebox_link_url', array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
) );
}
add_action( 'init', 'wb_register_block_meta' );
Run Code Online (Sandbox Code Playgroud)
JS
registerBlockType( 'ray/homebox-link-url', {
title: 'Homebox Link',
icon: 'universal-access-alt',
category: 'layout',
attributes: {
blockValue: {
type: 'string',
source: 'meta',
meta: …Run Code Online (Sandbox Code Playgroud) wordpress wordpress-theming wordpress-rest-api wordpress-gutenberg
我正在尝试使用the_content过滤器来处理 Divi 短代码,但短代码仍在出现。特别是et_pb_section、et_pb_column、et_pb_text,如果这很重要的话。
add_action( 'rest_api_init', function () {
register_rest_field(
'post',
'content',
array(
'get_callback' => 'ap3_divi_do_shortcodes',
'update_callback' => null,
'schema' => null,
)
);
register_rest_field(
'post',
'excerpt',
array(
'get_callback' => 'ap3_divi_do_shortcodes',
'update_callback' => null,
'schema' => null,
)
);
});
function ap3_divi_do_shortcodes( $object, $field_name, $request ) {
global $post;
$post = get_post($object['id']);
// Set is_singular to true to avoid "read more issue"
// Issue come from is_singular () in divi-builder.php line 73
global $wp_query;
$wp_query->is_singular = true;
$output = …Run Code Online (Sandbox Code Playgroud) 我想添加一个自定义字段以包含在我的搜索查询中。这是通过 完成的meta_query,我完全意识到这一点,但问题是,我不知道在哪里可以根据我的需要操纵查询参数。
所以我正在寻找一个过滤器,以进入 REST API 搜索请求 ( /wp/v2/search),有什么线索吗?
我试图弄清楚 Wordpress UserID 是否始终等于 WooCommerce CustomerID。
我需要找到此值来识别用户,但需要确保 WordPress 和 WooCommerce API 中的 ID 始终相同。
wordpress woocommerce wordpress-rest-api woocommerce-rest-api