• Resolved Babbelientje

    (@babbelientje)


    Hi all!

    I need some help… I want to set a standard signature below every blog post (not page) ánd a standard image on top, which sort of says ‘dear readers,’ in a fancy script. Adding the signature already worked, but I can’t figure out how to add the image on top… I don’t want it to be my featured image (obviously) and it has to appear below the page title, but above the blog text. Can you guys help me??

    BTW, the signature was added using this code in my functions.php:

    // Add Signature Image after single post
    add_filter('the_content','add_signature', 1);
    function add_signature($text) {
     global $post;
     if(($post->post_type == 'post')) 
        $text .= '<div class="signature"><img src="https://www.lievechaos.nl/wp/wp-content/uploads/2019/03/Liefs-xx_Tekengebied-1.jpg"></div>';
        return $text;
    }

    Thanks so much in advance!!

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Just add it to what you already have. You are making a sandwich and the existing content goes in the middle. (content does not include the title)
    $text = 'image' . $text . 'signature';

    And be sure to fix your filter to always return $text or it will mess up other post types (like Pages).

    Thread Starter Babbelientje

    (@babbelientje)

    OMG it worked! Thank you soooo much Joy. For a complete coding newbie like me it’s always this wonderful surprise when what you add to the code actually does what you hoped it would ??

    However, I don’t quite understand what you mean with your last remark, about filters and return $text? Could you maybe explain, please?

    A filter is a function that affects its parameter. A lot of the time, you only want to affect it under certain conditions. The way you have your function written, it will only return the text if it is a “post” post type. So if you look at a Page, you won’t see anything at all. It would get lost in the filter, because you didn’t return the original text.
    So you need to move the return $text to be outside the if statement. Filters should always return something. But filters can modify the original before it is returned.

    Edit: Actually, since you didn’t put braces around your if statement, then the return is always executed. Sorry, I had glanced at it and by indentation was thinking it had braces. Good coding practice is to always use braces for if statements.

    Thread Starter Babbelientje

    (@babbelientje)

    Ah, I understand! Learning a little more every day, and understanding if-statements adds huge potential to my coding skills. Thanks so much for your help ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Set standard image on top of blog post (but NOT featured)’ is closed to new replies.