0

I've made a theme that's a child of the standard Twenty Thirteen theme. Twenty Thirteen displays post_thumbnail images at the top of each of my pages, and I'd like my theme to not do this.

I could create my own version of all the templates that use it (like page.php, content.php) but this seems overkill for making such a small change.

I tried doing remove_theme_suport('post-thumbnail') but that had no effect.

I tried just deleting the add_theme_support('post-thumbnail') line from Twenty Thirteen's functions.php, but that had no effect.

I could hide the thumbnail using CSS, but the image is still loaded, and I'd prefer the HTML to not be included at all.

I don't want to go through loads of existing posts/pages and remove all the thumbnails I set ages ago.

Any thoughts? Is it possible to override has_post_thumbnail() so that it always returns FALSE?

NOTE: This is not the same as the "header" images Twenty Thirteen puts in the site-header block, but appears below the main navigation, in the main content of the page.

3

How can we force has_post_thumbnails() to return false?

Method #1 - disable theme support:

You can remove the post thumbnails support from the backend, with

function twentythirteen_child_setup()
{
    remove_theme_support( 'post-thumbnails' );
}

add_action( 'after_setup_theme', 'twentythirteen_child_setup', 11 );

but notice that this will not remove the featured images.

If the _thumbnail_id post meta value is non-empty, then has_post_thumbnails() will return true. So we need something else, in the that case.

Method #2 - a script to modify the database:

If you want to un-attach the featured images, you could use for example (untested):

/**
 * Un-attach all featured images:
 */
 $pids = get_posts( 
     array( 
        'post_type'       => array( 'post', 'page' ),
        'meta_key'        => '_thumbnail_id', 
         'fields'         => 'ids',
         'posts_per_page' => 10 // modify this to your needs  
     ) 
 );

 foreach( $pids as $pid )
 {
     // Delete:
     // delete_post_meta( $pid, '_thumbnail_id' ); 

     // Debug:
     printf( ' pid: %d <br/>', $pid );
 }

WARNING: Take a database backup before trying this code snippet.

Method #3 - hijack a filter:

If we go hunting for usable filters, then we are taken down the following path:

  • has_post_thumbnail() calls get_post_thumbnail_id()

  • get_post_thumbnail_id() calls get_post_meta()

  • get_post_meta() calls get_metadata('post', ... )

  • get_metadata() contains the get_{$meta_type}_metadata filter

So the filter to use is the get_post_metadata filter.

To hijack the returning value from the filter, $check must fulfill:

! is_null( $check ) && false === (bool) $check

for has_post_thumbnail() to return false.

To better understand this criteria, we can make some tests:

var_dump( ! is_null( 0     ) && false === (bool) 0      );
var_dump( ! is_null( false ) && false === (bool) false  );
var_dump( ! is_null( null  ) && false === (bool) null   );

which give the following output:

bool(true)
bool(true)
bool(false)

This means we can construct the following code snippet:

/**
 * Force has_post_thumbnail() to return false.
 */

! is_admin() && add_filter( 'get_post_metadata', 
    function( $check, $object_id, $meta_key, $single ) 
    {
        // Hijack the meta data for featured images:
        if ( '_thumbnail_id' === $meta_key )
        { 
            $check = 0; // or false
        }

        return $check;
    }
,10, 4 );

to force has_post_thumbnail() to return false.

You can place this code snippet in the functions.php file in your current theme directory or use it in your custom plugin.

ps: it looks like this has been solved few years ago, check for example this thread.

  • Thanks. So there isn't a way to not display already-applied featured images, other than removing them? – Phil Gyford Aug 9 '14 at 18:59
  • There might be a way with the get_post_metadata filter. See here in the source code. – birgire Aug 9 '14 at 19:06
  • Ah, my Googling missed that last wordpress.org post you linked to. That seems to do the trick! Many thanks for your efforts. If you want to post that code as a separate answer I'll mark it as correct. – Phil Gyford Aug 10 '14 at 8:45
  • @PhilGyford that's great, ps: my answer already contains a similar get_post_metadata filter example to force the has_thumbnails() to return false ;-) – birgire Aug 11 '14 at 11:12
  • But I didn't really understand how to use your answer, whereas the answer on wordpress.org had a function I could use in my functions.php as is. – Phil Gyford Aug 12 '14 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.