如何将类别添加到WordPress区块模式?

huangapple go评论128阅读模式
英文:

How to add categories to WordPress block patterns?

问题

我正在尝试为WP块模式创建分类。到目前为止,我有以下内容:

  1. // 为wp_block文章类型注册自定义分类法
  2. function register_patterns_taxonomy() {
  3. $labels = array(
  4. 'name' => '模式分类',
  5. 'singular_name' => '模式分类',
  6. 'search_items' => '搜索模式分类',
  7. 'all_items' => '所有模式分类',
  8. 'parent_item' => '父模式分类',
  9. 'parent_item_colon' => '父模式分类:',
  10. 'edit_item' => '编辑模式分类',
  11. 'update_item' => '更新模式分类',
  12. 'add_new_item' => '添加新模式分类',
  13. 'new_item_name' => '新模式分类名称',
  14. 'menu_name' => '模式分类',
  15. );
  16. $args = array(
  17. 'labels' => $labels,
  18. 'hierarchical' => true,
  19. 'public' => true,
  20. 'show_ui' => true,
  21. 'show_admin_column' => true,
  22. 'query_var' => true,
  23. 'show_in_rest' => true,
  24. 'rewrite' => array('slug' => 'patterns_categories'),
  25. );
  26. register_taxonomy('patterns_categories', array('wp_block'), $args);
  27. }
  28. add_action('init', 'register_patterns_taxonomy');
  29. // 通过分类术语创建块模式分类
  30. function create_block_pattern_category_for_terms() {
  31. $taxonomy = 'patterns_categories';
  32. $terms = get_terms(array(
  33. 'taxonomy' => $taxonomy,
  34. 'hide_empty' => false,
  35. ));
  36. foreach ($terms as $term) {
  37. $pattern_category = sanitize_title($term->name);
  38. $pattern_category_exists = get_terms('patterns_categories', array('slug' => $pattern_category));
  39. register_block_pattern_category(
  40. $term->slug,
  41. array('label' => __($term->name, 'wpdocs-my-plugin'))
  42. );
  43. }
  44. }
  45. add_action('init', 'create_block_pattern_category_for_terms');

这部分代码用于注册自定义分类法和创建块模式分类。

至于将这些术语添加到块模式中的部分,您尝试使用register_block_pattern()来创建块模式,但存在问题。以下是您尝试的代码:

  1. $args = array(
  2. 'post_type' => 'wp_block',
  3. 'posts_per_page' => -1,
  4. );
  5. $posts = get_posts($args);
  6. foreach ($posts as $post) {
  7. setup_postdata($post);
  8. $title = get_the_title();
  9. $content = get_the_content();
  10. // 获取与此文章相关的术语
  11. $terms = wp_get_post_terms($post->ID, 'patterns_categories', array('fields' => 'slugs'));
  12. // 创建块模式
  13. register_block_pattern(
  14. 'handy-options/' . $title,
  15. array(
  16. 'title' => $title,
  17. 'description' => _x('两个水平按钮,左边的按钮是填充的,右边的按钮是轮廓的。', '块模式描述', 'theme-slug'),
  18. 'categories' => $terms,
  19. 'content' => wp_json_encode($content),
  20. )
  21. );
  22. }
  23. wp_reset_postdata();

尽管新的块模式已经正确创建了,但您无法正确添加术语和内容。

这段代码的问题可能在于wp_json_encode($content)。您应该使用$content而不是将其编码,因为块模式应该接受原始内容而不是编码后的内容。尝试使用以下代码:

  1. 'content' => $content,

这应该能够正确地将内容添加到您的块模式中。希望这有所帮助!

英文:

I'm trying to create categories for WP block patterns. So far I have:

  1. // Custom taxonomy for wp_block post type
  2. function register_patterns_taxonomy() {
  3. $labels = array(
  4. 'name' => 'Pattern category',
  5. 'singular_name' => 'Pattern category',
  6. 'search_items' => 'Search Patterns categories',
  7. 'all_items' => 'All Patterns categories',
  8. 'parent_item' => 'Parent Pattern category',
  9. 'parent_item_colon' => 'Parent Pattern category:',
  10. 'edit_item' => 'Edit Pattern category',
  11. 'update_item' => 'Update Pattern category',
  12. 'add_new_item' => 'Add New Pattern category',
  13. 'new_item_name' => 'New Pattern category Name',
  14. 'menu_name' => 'Patterns categories',
  15. );
  16. $args = array(
  17. 'labels' => $labels,
  18. 'hierarchical' => true,
  19. 'public' => true,
  20. 'show_ui' => true,
  21. 'show_admin_column' => true,
  22. 'query_var' => true,
  23. 'show_in_rest' => true,
  24. 'rewrite' => array( 'slug' => 'patterns_categories' ),
  25. );
  26. register_taxonomy( 'patterns_categories', array( 'wp_block' ), $args );
  27. }
  28. add_action( 'init', 'register_patterns_taxonomy' );
  29. // creating patters categories by taxonomy terms
  30. function create_block_pattern_category_for_terms() {
  31. $taxonomy = 'patterns_categories'; // Zmień na nazwę swojej taksonomii
  32. $terms = get_terms( array(
  33. 'taxonomy' => $taxonomy,
  34. 'hide_empty' => false, // Pobierz wszystkie termy, także te puste
  35. ) );
  36. foreach ( $terms as $term ) {
  37. $pattern_category = sanitize_title( $term->name ); // Tworzenie unikalnej nazwy kategorii
  38. $pattern_category_exists = get_terms( 'patterns_categories', array( 'slug' => $pattern_category ) );
  39. register_block_pattern_category(
  40. $term->slug,
  41. array( 'label' => __( $term->name, 'wpdocs-my-plugin' ) )
  42. );
  43. }
  44. }
  45. 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:

  1. // trying to create patterns
  2. $args = array(
  3. 'post_type' => 'wp_block',
  4. 'posts_per_page' => -1,
  5. );
  6. $posts = get_posts($args);
  7. foreach ($posts as $post) {
  8. setup_postdata($post);
  9. $title = get_the_title();
  10. $content = get_the_content();
  11. // Fetch terms associated with this post
  12. $terms = wp_get_post_terms($post->ID, 'patterns_categories', array('fields' => 'slugs'));
  13. // Create block pattern
  14. register_block_pattern(
  15. 'handy-options/' . $title,
  16. array(
  17. 'title' => $title,
  18. 'description' => _x('Two horizontal buttons, the left button is filled in, and the right button is outlined.', 'Block pattern description', 'theme-slug'),
  19. 'categories' => $terms,
  20. 'content' => wp_json_encode($content),
  21. )
  22. );
  23. }
  24. 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,它就会正常显示。

  1. add_action( 'load-post.php', 'register_theme_patterns' );
  2. add_action( 'load-post-new.php', 'register_theme_patterns' );
  3. function register_theme_patterns()
  4. {
  5. if ( !function_exists( 'register_block_pattern' ) )
  6. {
  7. return;
  8. }
  9. // 查询所有已发布的模式。
  10. $patterns = new WP_Query( [
  11. 'post_type' => 'wp_block',
  12. 'number_posts' => -1
  13. ] );
  14. if ( $patterns->have_posts() )
  15. {
  16. while ( $patterns->have_posts() )
  17. {
  18. $patterns->the_post();
  19. global $post;
  20. $terms = wp_get_post_terms( $post->ID, 'patterns_categories', array( 'fields' => 'slugs' ) );
  21. register_block_pattern(
  22. sprintf( 'theme-patterns/%s', sanitize_key( $post->post_name ) ),
  23. [
  24. 'title' => wp_strip_all_tags( $post->post_title ),
  25. 'content' => $post->post_content,
  26. 'categories' => $terms
  27. ]
  28. );
  29. }
  30. }
  31. wp_reset_postdata();
  32. }
英文:

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.

  1. add_action( 'load-post.php', 'register_theme_patterns' );
  2. add_action( 'load-post-new.php', 'register_theme_patterns' );
  3. function register_theme_patterns()
  4. {
  5. if ( !function_exists( 'register_block_pattern' ) )
  6. {
  7. return;
  8. }
  9. // Query all published patterns.
  10. $patterns = new WP_Query( [
  11. 'post_type' => 'wp_block',
  12. 'number_posts' => -1
  13. ] );
  14. if ( $patterns->have_posts() )
  15. {
  16. while ( $patterns->have_posts() )
  17. {
  18. $patterns->the_post();
  19. global $post;
  20. $terms = wp_get_post_terms( $post->ID, 'patterns_categories', array( 'fields' => 'slugs' ) );
  21. register_block_pattern(
  22. sprintf( 'theme-patterns/%s', sanitize_key( $post->post_name ) ),
  23. [
  24. 'title' => wp_strip_all_tags( $post->post_title ),
  25. 'content' => $post->post_content,
  26. 'categories' => $terms
  27. ]
  28. );
  29. }
  30. }
  31. wp_reset_postdata();
  32. }
  33. </details>

huangapple
  • 本文由 发表于 2023年8月11日 01:57:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定