我已创建自定义帖子类型ml_patterns_
. 现在,我想使用每个帖子作为单独的古腾堡块模式。为此,我创建了以下内容:
$args = array(
\'post_type\' => \'ml_patterns_\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 9999,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
add_action( \'init\', function() {
$title = __( get_the_title(), \'ml_patterns\' ); // post\'s name is the title of the pattern (label)
$titleSlug = \'ml_patterns/ml_pattern_\'.get_the_ID(); // post\'s slug is the name of the pattern
$patternContent = get_the_content(); // the post content as pattern content
register_block_pattern(
$titleSlug,
array(
\'title\' => $title,
\'content\' => trim($patternContent),
\'categories\' => array( \'ml_test\' ),
)
);
});
endwhile;
wp_reset_postdata();
工作很好,但我只使用一种模式,即使有多个帖子。我的错在哪里?