0

In my single.php (my template Luna) I have this code:

<?php if ( has_post_thumbnail( $post->ID ) ): ?>    

        <header class="page-entry-header" style="background-image: url('<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID) , 'max-control' ); ?>');">

    <?php endif; ?>  

This show header with background image = featured image.

I want to remove the background (featured image - delete just in post) but not via single.php I want to remove via function.php, this is possible ?

¿How could I do this.?

I appreciate your help.

8
  • 1
    yes it is. If you meant to ask how to do it, please edit the question to clarify it. Oct 10 '15 at 12:45
  • your best bet is via css than a function, a single post will have a body class of .single so just use .single header {display:none;} Doing it via the functions.php will require a lot more code as you will need to check if the code exists and the template is the right one etc. Oct 10 '15 at 14:17
  • If I do via css, the file is loaded in dom, I not want to do that. Oct 10 '15 at 14:35
  • 1
    You could probably do it via a somewhat complex filter but the CSS solution would be easiest.
    – s_ha_dum
    Oct 10 '15 at 17:55
  • 1
    @s_ha_dum Your filter could still fire in unintended ways, not really. How you use and manipulate the parameters is the key. My example code is very basic, it is up to the OP to adjust to exact needs Oct 11 '15 at 15:07
0

I'm not sure why you cannot just create a child theme and then simply copy the single template to your child theme and remove the code, but anyways, this is still a very interesting approach.

Regardless what have been said in comments using CSS to hide the thumbnail (which I do not support as it will still show in the browser source), there are better programmatic ideas to get rid of post thumbnails.

The best idea here would be to get the has_post_thumbnail() conditional return false only on single pages. To do this is super easy, we just need to follow the source code. Here is how it works

The {$meta_type} part will be post as we will be using this filter to target posts, so our filter will be get_post_metadata.

So, in this filter, we need to set the first parameter $value to false. From the source

Returning a non-null value will effectively short-circuit the function.

We would also only want to trigger this filter only for single posts and for post thumbnails, to will will check if we are on a single page and that our current $meta_key is _thumbnail_id which is the custom field key where the post thumbnail ID is stored.

With this all in mind, you can try the following

add_filter( 'get_post_metadata', function ( $value, $object_id, $meta_key )
{
    if (    is_single() // Make sure we are on a single post page
         && $meta_key === '_thumbnail_id' // Only run the filter for post thumbnails
    )
        return false; // Breaks the function which in turn return false for has_post_thumbnail()

    return $value;
}, 10, 3 );
3
  • Thanks for your code, this works. But I have one problem with editor dreamweaver cs5, when I paste the code in function.php I get this alert: There is a syntax error on line 302. Code hinting may not work until you fix this error. Oct 11 '15 at 15:41
  • My pleasure, glad it worked. Your dreamweaver version is too old and not yet compatible with closures that was introduced in PHP 5.3, that is why you get the error. You should upgrade your dreamweaver to the latest version ;-) Oct 11 '15 at 15:55
  • 1
    Thank you, Have a good day, I will see this of dreamweaver version. Oct 11 '15 at 16:00

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.