0
votes

Wordpress 4.3.1

Hi,

this is my first post at stackoverflow and I hope I did everything right.

I need a function that allows to output the Featured Image, or not. Just not uploading an image is no option, because the thumbnail next the excerpts is needed in any case.

The following combinations are needed:

  1. Thumbnail and Featured Image are identical
  2. Thumbnail and first image in the article are different
  3. Thumbnail and no Featured Image

I need the function for case 2 and 3. In case 2, the Featured Image will be integrated via the editor.

I have already found this plugin: https://wordpress.org/plugins/hide-featured-image/

However, that solves the task only with CSS:

.has-post-thumbnail img.wp-post-image{ display: none; }

But I would like that the picture is not even putted out and loaded in the Browser.

I have the following solution found on the net. It is working so far that the checkbox is shown inside the Featured Image box and it also saves the state if it is checked, or not, after updating the article. However, the Featured Image continues to be output and I don't understand what is going wrong.

Here is the code that I copied to the functions.php:

/**
 * 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 );
}  

And here's the WP snippet, I use at the template for the output:

<?php the_post_thumbnail('header-image'); ?> 

I'm grateful for any advice! Thank you in advance for your effort!

1

1 Answers

1
votes

Firstly I'd check in the database that this 'optional_featured_image' parameter is actually being set by your code. Check the prefix_postmeta table and lookup for the post you're editing.

When the template is output you need to have a conditional to check for this 'display_featured_image' parameter you've setup. Otherwise the_post_thumbnail() will always display.

Try this code in your template:

<?php
$optional_featured_image = get_post_meta( get_the_ID(), 'display_featured_image', true);
if( !$optional_featured_image )
    the_post_thumbnail('header-image');
?>