Skip to content

Instantly share code, notes, and snippets.

@manishsongirkar
Created May 22, 2023 11:10
Show Gist options
  • Save manishsongirkar/a338186d1e6bca500be9dd1999af5498 to your computer and use it in GitHub Desktop.
Save manishsongirkar/a338186d1e6bca500be9dd1999af5498 to your computer and use it in GitHub Desktop.
Toggle option below featured image to show/hide featured image on frontend
<?php
/**
* Register post meta for featured image display.
*/
function register_block_post_meta() {
register_post_meta(
'post',
'featuredImageDisplay',
[
'show_in_rest' => true,
'single' => true,
'default' => true,
'type' => 'boolean',
'description' => __( 'Display toggle option to show/hide featured image.', 'text-domain' ),
]
);
}
add_action( 'init', 'register_block_post_meta' );
<?php
// Other single post code...
$featured_image_display = get_post_meta( get_the_ID(), 'featuredImageDisplay', true );
if ( has_post_thumbnail() && $featured_image_display ) {
the_post_thumbnail( 'full' );
}
// Other single post code...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment