JavaScript for WordPress › Forums › Gutenberg Development › Dynamic block not showing in frontend › Reply To: Dynamic block not showing in frontend
April 19, 2018 at 6:32 am
#45398
ataurr
Participant
I have added this to index.php but still nothing showing in front
function my_plugin_render_block_latest_post( $attributes ) {
$recent_posts = wp_get_recent_posts( array(
'numberposts' => 1,
'post_status' => 'publish',
) );
if ( count( $recent_posts ) === 0 ) {
return 'No posts';
}
$post = $recent_posts[ 0 ];
$post_id = $post['ID'];
return sprintf(
'<a class="wp-block-my-plugin-latest-post" href="%1$s">%2$s</a>',
esc_url( get_permalink( $post_id ) ),
esc_html( get_the_title( $post_id ) )
);
}
function jsforwpblocks_register_blocks() {
// Hook server side rendering into render callback
register_block_type( 'gutenbergtest/dynamicblock', array(
'render_callback' => 'my_plugin_render_block_latest_post',
) );
register_meta( 'post', 'subheading', [
'show_in_rest' => true,
] );
}
// Make sure that Gutenberg is available
if ( function_exists( 'register_block_type' ) ) {
add_action( 'init', 'jsforwpblocks_register_blocks' );
}
I Am really confused what I am missing. I have searched on google nothing found any solution about this.
Its showing in editor
Nothing showing in frontend