Skip to content

Instantly share code, notes, and snippets.

@rocket-martue
Last active October 11, 2021 08:06
Show Gist options
  • Save rocket-martue/5a2cd7d491f2b1f9ed8b30cd6f31960b to your computer and use it in GitHub Desktop.
Save rocket-martue/5a2cd7d491f2b1f9ed8b30cd6f31960b to your computer and use it in GitHub Desktop.
再利用ブロックをスラッグで表示するショートコード
<?php
// [reusable slug=$slug]
add_shortcode(
'reusable',
function ( $atts ) {
extract( shortcode_atts(
array(
'slug' => '',
), $atts ) );
ob_start();
$args = array(
'post_type' => array( 'wp_block' ),
'name' => $slug,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
the_content();
}
}
wp_reset_postdata();
$html = ob_get_contents();
ob_end_clean();
return $html;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment