英文:
How to add categories to WordPress block patterns?
问题
我正在尝试为WP块模式创建分类。到目前为止,我有以下内容:
// 为wp_block文章类型注册自定义分类法
function register_patterns_taxonomy() {
$labels = array(
'name' => '模式分类',
'singular_name' => '模式分类',
'search_items' => '搜索模式分类',
'all_items' => '所有模式分类',
'parent_item' => '父模式分类',
'parent_item_colon' => '父模式分类:',
'edit_item' => '编辑模式分类',
'update_item' => '更新模式分类',
'add_new_item' => '添加新模式分类',
'new_item_name' => '新模式分类名称',
'menu_name' => '模式分类',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'show_in_rest' => true,
'rewrite' => array('slug' => 'patterns_categories'),
);
register_taxonomy('patterns_categories', array('wp_block'), $args);
}
add_action('init', 'register_patterns_taxonomy');
// 通过分类术语创建块模式分类
function create_block_pattern_category_for_terms() {
$taxonomy = 'patterns_categories';
$terms = get_terms(array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
));
foreach ($terms as $term) {
$pattern_category = sanitize_title($term->name);
$pattern_category_exists = get_terms('patterns_categories', array('slug' => $pattern_category));
register_block_pattern_category(
$term->slug,
array('label' => __($term->name, 'wpdocs-my-plugin'))
);
}
}
add_action('init', 'create_block_pattern_category_for_terms');
这部分代码用于注册自定义分类法和创建块模式分类。
至于将这些术语添加到块模式中的部分,您尝试使用register_block_pattern()
来创建块模式,但存在问题。以下是您尝试的代码:
$args = array(
'post_type' => 'wp_block',
'posts_per_page' => -1,
);
$posts = get_posts($args);
foreach ($posts as $post) {
setup_postdata($post);
$title = get_the_title();
$content = get_the_content();
// 获取与此文章相关的术语
$terms = wp_get_post_terms($post->ID, 'patterns_categories', array('fields' => 'slugs'));
// 创建块模式
register_block_pattern(
'handy-options/' . $title,
array(
'title' => $title,
'description' => _x('两个水平按钮,左边的按钮是填充的,右边的按钮是轮廓的。', '块模式描述', 'theme-slug'),
'categories' => $terms,
'content' => wp_json_encode($content),
)
);
}
wp_reset_postdata();
尽管新的块模式已经正确创建了,但您无法正确添加术语和内容。
这段代码的问题可能在于wp_json_encode($content)
。您应该使用$content
而不是将其编码,因为块模式应该接受原始内容而不是编码后的内容。尝试使用以下代码:
'content' => $content,
这应该能够正确地将内容添加到您的块模式中。希望这有所帮助!
英文:
I'm trying to create categories for WP block patterns. So far I have:
// Custom taxonomy for wp_block post type
function register_patterns_taxonomy() {
$labels = array(
'name' => 'Pattern category',
'singular_name' => 'Pattern category',
'search_items' => 'Search Patterns categories',
'all_items' => 'All Patterns categories',
'parent_item' => 'Parent Pattern category',
'parent_item_colon' => 'Parent Pattern category:',
'edit_item' => 'Edit Pattern category',
'update_item' => 'Update Pattern category',
'add_new_item' => 'Add New Pattern category',
'new_item_name' => 'New Pattern category Name',
'menu_name' => 'Patterns categories',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'show_in_rest' => true,
'rewrite' => array( 'slug' => 'patterns_categories' ),
);
register_taxonomy( 'patterns_categories', array( 'wp_block' ), $args );
}
add_action( 'init', 'register_patterns_taxonomy' );
// creating patters categories by taxonomy terms
function create_block_pattern_category_for_terms() {
$taxonomy = 'patterns_categories'; // Zmień na nazwę swojej taksonomii
$terms = get_terms( array(
'taxonomy' => $taxonomy,
'hide_empty' => false, // Pobierz wszystkie termy, także te puste
) );
foreach ( $terms as $term ) {
$pattern_category = sanitize_title( $term->name ); // Tworzenie unikalnej nazwy kategorii
$pattern_category_exists = get_terms( 'patterns_categories', array( 'slug' => $pattern_category ) );
register_block_pattern_category(
$term->slug,
array( 'label' => __( $term->name, 'wpdocs-my-plugin' ) )
);
}
}
add_action( 'init', 'create_block_pattern_category_for_terms' );
and that works fine, I have custom taxonomy visible in block pattern post, also I successfully created blocks pattern categories based on custom taxonomy terms.
But now, I need to add somehow these terms to blocks patterns.
My idea is using register_block_pattern() for each post in wp_block post type, and add properly: title, content and categories. So I've tried:
// trying to create patterns
$args = array(
'post_type' => 'wp_block',
'posts_per_page' => -1,
);
$posts = get_posts($args);
foreach ($posts as $post) {
setup_postdata($post);
$title = get_the_title();
$content = get_the_content();
// Fetch terms associated with this post
$terms = wp_get_post_terms($post->ID, 'patterns_categories', array('fields' => 'slugs'));
// Create block pattern
register_block_pattern(
'handy-options/' . $title,
array(
'title' => $title,
'description' => _x('Two horizontal buttons, the left button is filled in, and the right button is outlined.', 'Block pattern description', 'theme-slug'),
'categories' => $terms,
'content' => wp_json_encode($content),
)
);
}
wp_reset_postdata();
but I don't know is this a good way - although new block patters are created with correct title, I can't add terms and content correctly.
Any advice, idea?
答案1
得分: 0
以下是您要翻译的内容:
当您来注册您的块模式时,最好将其添加到一个挂载点上,该挂载点仅在编辑或添加文章/页面等情况下加载模式。
主要错误是JSON编码的内容。只需添加post_content,它就会正常显示。
add_action( 'load-post.php', 'register_theme_patterns' );
add_action( 'load-post-new.php', 'register_theme_patterns' );
function register_theme_patterns()
{
if ( !function_exists( 'register_block_pattern' ) )
{
return;
}
// 查询所有已发布的模式。
$patterns = new WP_Query( [
'post_type' => 'wp_block',
'number_posts' => -1
] );
if ( $patterns->have_posts() )
{
while ( $patterns->have_posts() )
{
$patterns->the_post();
global $post;
$terms = wp_get_post_terms( $post->ID, 'patterns_categories', array( 'fields' => 'slugs' ) );
register_block_pattern(
sprintf( 'theme-patterns/%s', sanitize_key( $post->post_name ) ),
[
'title' => wp_strip_all_tags( $post->post_title ),
'content' => $post->post_content,
'categories' => $terms
]
);
}
}
wp_reset_postdata();
}
英文:
When you come to register your block pattern, it's best to add that to a hook which loads the patterns only when editing or adding a post/page etc.
The main error is the JSON-encoded content. Just add the post_content, and it'll display just fine.
add_action( 'load-post.php', 'register_theme_patterns' );
add_action( 'load-post-new.php', 'register_theme_patterns' );
function register_theme_patterns()
{
if ( !function_exists( 'register_block_pattern' ) )
{
return;
}
// Query all published patterns.
$patterns = new WP_Query( [
'post_type' => 'wp_block',
'number_posts' => -1
] );
if ( $patterns->have_posts() )
{
while ( $patterns->have_posts() )
{
$patterns->the_post();
global $post;
$terms = wp_get_post_terms( $post->ID, 'patterns_categories', array( 'fields' => 'slugs' ) );
register_block_pattern(
sprintf( 'theme-patterns/%s', sanitize_key( $post->post_name ) ),
[
'title' => wp_strip_all_tags( $post->post_title ),
'content' => $post->post_content,
'categories' => $terms
]
);
}
}
wp_reset_postdata();
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论