Hi,
ich benötige eine Funktion, mit der man das Beitragsbild bei bestimmten Posts per Checkbox ausblenden kann. Ich habe bereits dieses Plugin dazu gefunden: https://wordpress.org/plugins/hide-featured-image/
Allerdings löst das die Aufgabe lediglich mit
Ich möchte aber, dass das Bild erst gar nicht ausgegeben und somit im Broser geladen wird.
Ich habe noch folgende Lösung im Netz gefunden. Die Checkbox wird auch ausgegeben und merkt sich nach dem speichern, wenn sie angehakt ist. Allerdings wird das Beitragsbild weiterhin ausgegeben. Ich konmme einfach nicht auf den Fehler. Hier der Code, den ich in die functions.php kopiert habe:
/**
* Adds a checkbox to the featured image metabox.
*
* @param string $content
*/
add_filter( 'admin_post_thumbnail_html', 'optional_featured_image', 10, 2 );
/**
* Add a 'hide the featured image' checkbox to the Featured Image area
*/
function optional_featured_image( $content, $post_ID ) {
$content .= '<div class="hide-feat-image">';
// add our nonce field
$content .= wp_nonce_field( 'display_featured_image_nonce', 'display_featured_image_nonce', true, false );
// create our checkbox
$content .= '
<input style="margin: 5px;" type="checkbox" id="display-featured-image" name="display_featured_image" '. checked( get_post_meta( $post_ID, 'display_featured_image', true ), true, false ) .' value="1"/>
<label for="display-featured-image">Hide on post page?</label>
';
$content .= '</div><!-- hide-feat-image -->';
// return our appended $content
return $content;
}
add_action( 'save_post', 'save_optional_featured_image' );
/**
* Save our 'hide the featured image' checkbox to the Featured Image area
*/
function save_optional_featured_image( $post_id ) {
if (
// check nonce
!isset( $_POST['display_featured_image_nonce'] )
|| !wp_verify_nonce( $_POST['display_featured_image_nonce'], 'display_featured_image_nonce')
// make sure user is allowed to edit posts
|| !current_user_can( 'edit_post', $post_id )
)
return;
// sanitize our input to yes or no
$display = isset( $_POST["display_featured_image"] ) && $_POST["display_featured_image"] ? true : false;
// update our post
update_post_meta( $post_id, 'display_featured_image', $display );
}
Alles anzeigen
Und hier noch das WP-Snippet, das ich im Template für die Ausgabe verwende:
Installiert ist WP 4.3.1
Bin für jeden Hinweis dankbar!