Skip to content

Snippet: Add Meta Option To Portfolio Posts For Disabling Auto Media

// Add option to Portfolio items to disable media
function my_new_portfolio_meta_options( $options ) {

	// Add new setting to portfolio tab
	$options['portfolio']['settings']['wpex_post_media'] = array(
		'title'       => __( 'Post Media', 'wpex' ),
		'description' => __( 'Display media on single post?', 'wpex' ),
		'id'          => 'wpex_post_media',
		'type'        => 'select',
		'options'     => array(
			''   => __( 'Yes', 'wpex' ),
			'no' => __( 'No', 'wpex' ),
		),
	);

	// Return all options
	return $options;

}
add_filter( 'wpex_metabox_array', 'my_new_portfolio_meta_options', 40 );

// Disable portfolio post media by tweaking single blocks if disabled via custom-field
function my_portfolio_media_visibility( $blocks ) {

	// Set block vals equal to keys for ease
	$blocks = array_combine( $blocks, $blocks );

	// If wpex_post_media field is set to 'no' then remove the media
	if ( 'no' == get_post_meta( get_the_ID(), 'wpex_post_media', true ) ) {
		unset( $blocks['media'] );
	}

	// Return layout blocks
	return $blocks;

}
add_filter( 'totaltheme/portfolio/single_blocks', 'my_portfolio_media_visibility', 20 );
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