Hvis du vil vise/skjule featured image i en post, så er denne metode ganske enkel…
(Brug instpector på titlen ovenfor, så kan du se class’en ‘featured-skjul’ på DIV elementet lige ovenfor)
Du skal have fat i functions.php i dit Child Theme og tilføje koden her:
/* Define the custom box */
add_action( 'add_meta_boxes', 'balles_add_custom_box' );
/* Do something with the data entered */
add_action( 'save_post', 'balles_save_postdata' );
/* Adds a box to the main column on the Post and Page edit screens */
function balles_add_custom_box() {
add_meta_box(
'balles_sectionid',
'Hide featured image',
'balles_inner_custom_box',
'post',
'side',
'high'
);
}
/* Prints the box content */
function balles_inner_custom_box($post)
{
// Use nonce for verification
wp_nonce_field( 'balles_field_nonce', 'balles_noncename' );
// Get saved value, if none exists, "default" is selected
$saved = get_post_meta( $post->ID, 'hide_thumb', true);
if( !$saved )
$saved = 'default';
$fields = array(
'vis' => __('Vis', 'balles'),
'skjul' => __('Skjul', 'balles'),
);
foreach($fields as $key => $label)
{
printf(
'<input type="radio" name="hide_thumb" value="%1$s" id="hide_thumb[%1$s]" %3$s />'.
'<label for="hide_thumb[%1$s]"> %2$s ' .
'</label><br>',
esc_attr($key),
esc_html($label),
checked($saved, $key, false)
);
}
}
/* When the post is saved, saves our custom data */
function balles_save_postdata( $post_id )
{
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['balles_noncename'], 'balles_field_nonce' ) )
return;
if ( isset($_POST['hide_thumb']) && $_POST['hide_thumb'] != "" ){
update_post_meta( $post_id, 'hide_thumb', $_POST['hide_thumb'] );
}
}
Kopier single.php til dit child-theme og erstat linjen(36):
<div class="et_post_meta_wrapper"></div>
med:
<?php $hide_thumb = get_post_meta( get_the_ID(), 'hide_thumb', true); ?> <div class="et_post_meta_wrapper featured-<?php echo esc_attr($hide_thumb); ? >"></div>
Så mangler kun CSS. Tilføj til styles.css
.et_post_meta_wrapper.featured-skjul > img:nth-of-type(1) {
display: none !important;
}