• Resolved demonboy

    (@demonboy)


    Hi,

    I’m currently using the following code in my functions.php file:

    add_filter ('the_content', 'my_content_filter');
    
    function my_content_filter ($content) {
        $extra_html = '<p>Link to my image</p>';
        return $content.$extra_html;
    }

    This works but it is adding this on every page and post. I only wish to include this on my blog posts. I tried changing ‘the_content’ to ‘the_post’ but that didn’t work. I know this is really simple but I’m a bit rusty!

    Any pointers would be appreciated, thank you.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Sohil Vahora

    (@sohilvahora96)

    Hi @demonboy,

    Please use the below code to insert the same image on the content of every post, It will definitely work.

    === Start code ===
    function insert_image_into_posts($content) {
    // Get the ID of the image you want to insert
    $image_id = 123; // Replace 123 with the ID of your image

    // Check if it’s a post and not a page
    if (is_single() && get_post_type() === ‘post’) {
    // Insert the image into the post content
    $image_html = wp_get_attachment_image($image_id, ‘full’);
    $content = $image_html . ‘
    ‘ . $content;
    }
    return $content;
    }
    add_filter(‘the_content’, ‘insert_image_into_posts’);
    === End code ===

    – To get the image ID, go to the WordPress admin panel, visit the media page, and click on your selected image, and in the URL you can get the ID of the image.

    Thanks

    Thread Starter demonboy

    (@demonboy)

    Hi Sohil, thank you for that, it’s much appreciated. Where would I add any html in this code? For example, if I wanted to link the image or include a paragraph of text before it? Something like this…

    $image_html = <p>some text</p><a href=”mylink.com”><img src=wp_get_attachment_image($image_id, ‘full’)</a>;

    Moderator bcworkz

    (@bcworkz)

    You place any HTML and plain text in quotes and use the concatenation operator ..
    $image_html = '<p>some text</p><a href="mylink.com">'. wp_get_attachment_image($image_id, 'full') .'</a>';

    Note that wp_get_attachment_image() returns a full img tag. wp_get_attachment_image_url() returns just the URL itself.

    Mind the quotes in any code here, they cannot be the “curly” kind, they need to be the 'straight' kind.

    @sohilvahora96 – thanks for the useful code! Because the forum’s parser converts most quotes to the curly kind, next time you post any code, please use the code block or demarcate with backticks to prevent your code’s quotes from being corrupted.

    Thread Starter demonboy

    (@demonboy)

    OK, this isn’t quite working. Collecting the above, I have this code:

    /** add image at end of blog post **/
    function insert_image_into_posts($content) {
    // Get the ID of the image you want to insert
    $image_id = 54771; 
    
    // Check if it’s a post and not a page
    if (is_single() && get_post_type() === 'post') {
    // Insert the image into the post content
    $image_html = '<p>my content.</p><a href="">'.
    wp_get_attachment_image($image_id, 'full'); .'</a>';
    $content = $image_html .'
    '. $content;
    }
    return $content;
    }
    add_filter('the_content', 'insert_image_into_posts');
    

    WordPress is saying that the ‘.’ before ‘</a> is unexcepted. This one:

    <code role="textbox" aria-multiline="true" aria-label="Code" contenteditable="true" class="block-editor-rich-text__editable rich-text" style="white-space: pre-wrap; min-width: 1px;">wp_get_attachment_image($image_id, 'full'); .'</a>';

    If I remove it then of course all following text becomes a link. Where should this ‘.’ go?

    Secondly, it is not inserting the image at the end of the post, it’s thrown it in randomly after the first paragraph.

    Any pointers?

    Moderator bcworkz

    (@bcworkz)

    It’s not the . that’s the problem, it’s the ; which occurs just before it. Remove it. The trouble with some error messages is they tell us where PHP noticed a problem, not necessarily where the problem really is. Sometimes we need to look before where the error supposedly took place. Here it was just one character before. Sometimes it can be the line before or even earlier.

    This line says add the image before other content:
    $content = $image_html .' '. $content;

    If you want the image afterwards reverse it:
    $content = $content .' '. $image_html;
    This does the same thing more concisely, FWIW:
    $content .= " $image_html";
    The double quotes are important to ensure the space is part of the output. The ability to place string variables within a double quoted string is an unusual feature of PHP.

    it’s thrown it in randomly after the first paragraph.

    The above change should fix this, but it’s odd to randomly occur after the first paragraph. There could be other filter code inserting content. To help ensure your code runs after other filter code, add your callback with a large $priority argument:
    add_filter('the_content', 'insert_image_into_posts', 9999 );

    Thread Starter demonboy

    (@demonboy)

    @bcworkz – thank you so much for that. I should have spotted the image and content mix-up but it would have taken me a long time to spot the other problem. It is now working, cheers, and I appreciate your time. Have a great weekend!

    Thread Starter demonboy

    (@demonboy)

    @bcworkz – hi again. I have one more question. How can I exclude posting this image on specific category posts?

    Moderator bcworkz

    (@bcworkz)

    You need to get the post’s assigned categories with wp_get_post_categories(). For the $args parameter, pass ['fields'=>'ids',] so it returns just an array of assigned IDs instead of term objects. For the $post_id parameter, pass get_the_ID() to get the current post’s ID.

    Make an array of category term IDs that you want to exclude. Compare the two arrays. Exactly how depends on what sort of logic you want to apply, for example, match all or match any. PHP has various array comparison functions such as array_diff() which you can use to help with this. The simplest case is if there is only one category you wish to exclude. You can then use in_array() to see if that ID is in the array of assigned IDs.

    Depending on the result of the comparison, either return $content unchanged, or return $content with $image_html appended.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Insert same image on every post, not page’ is closed to new replies.