Skip to content

Snippet: Hide Blog Date & Add Page Setting to Conditionally Enable It

The following snippet can be used to hide the post meta data from blog posts and entries and only display it when a custom field has been checked to show it.

// Add new page setting
add_filter( 'wpex_metabox_array', function( $array, $post_type ) {
	if ( 'post' !== $post_type ) {
		return $array; // Add new setting to main tab for blog posts only
	}

	$array['main']['settings']['wpex_display_date'] = array(
		'title'       => __( 'Show Date?', 'wpex' ),
		'description' => __( 'Display date on entries and posts', 'wpex' ),
		'id'          => 'wpex_display_date',
		'type'        => 'select',
		'options'     => array(
			''      => __( 'No', 'wpex' ), // Default is no so it has an empty value
			'yes'   => __( 'Yes', 'wpex' ),
		),
	);

	return $array;

}, 2, 20 );

// Show/hide date from meta.
function wpex_blog_meta_sections( $sections ) {
	if ( 'yes' !== get_post_meta( get_the_ID(), 'wpex_display_date', true ) ) {
		unset( $sections['date'] );
	}
	return $sections;
}
add_filter( 'totaltheme/blog/meta_blocks/entry_blocks', 'wpex_blog_meta_sections' );
add_filter( 'totaltheme/blog/meta_blocks/single_blocks', 'wpex_blog_meta_sections' );
All PHP snippets should be added via child theme's functions.php file or via a plugin.
We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top