Modification to single.php for a post footer
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I want to include this banner <a href="https://www.facebook.com/pages/abc"><img src="http://shuttermuse.com/assets/images/Facebook_newsletter-banner.jpg" /></a> at the bottom of every post on my site. I want it to go beneath the content of the post but above the comments and above the related post plugin that is running on there. The site is shuttermuse.com/test.

At the moment I'm running a plugin called 'bottom of every post" but for some reason it will not allow me to center the banner using so the banner appears in the correct place on the page but will not center. I figure rather than messing around with the plugin it's probably better to just add the code directly to single.php at this point but when I tried that I could not get it to appear in quite the right place.

thanks!

awarded to alixaxel

Crowdsource coding tasks.

1 Solution

Winning solution

Can't you encapsulate that code snippet in a div?

<div align="center">
    <a href="https://www.facebook.com/pages/abc">
        <img src="http://shuttermuse.com/assets/images/Facebook_newsletter-banner.jpg" />
    </a>
</div>

If not, mind sharing a direct URL for a concrete example of what's going wrong?


The post and the "You may also like" segment come from the following line of code:

get_template_part('content', get_post_format());

To insert something in between these sections, you need to register a new the_content filter (which exactly what the "Bottom of every post" plugin does), like this:

$html = <<<BANNER
    <div align="center">
        <a href="https://www.facebook.com/pages/abc">
            <img src="http://shuttermuse.com/assets/images/Facebook_newsletter-banner.jpg" />
        </a>
    </div>
BANNER;

add_filter('the_content', function ($content) use ($html)
{
    return $content . $html;
});

The only benefit you gain from this is that contrary to the "Bottom of every post" plugin, you don't need to read a file from the disk on every post load. The performance increase is insignificant and I would advise you to keep using the plugin instead (as you can easily enable / disable it when needed) - however, if you want, you can find the modified single.php code on https://gist.github.com/anonymous/b1c40f8e828e9e2e71bb.


I suppose the correct way to do what you want would be to update the files:

wp-content/themes/gridspace/content-post.php
wp-content/themes/gridspace/content.php

And add the HTML snippet there (note that I'm guessing the paths).

This approach doesn't require a plugin nor a new registered filter.

The Gridspace theme you're using is closed source, so I'm unable to check / do it out for you.

It should be trivial, but if you need help doing that, please share the files. =)

Also, please share the single.php source code.
alixaxel over 6 years ago
Yeah I was doing that but..... anyway as I said I figured I might as well just modify the single.php file instead. Run one less plugin. So what I'm looking for here is how to modify the single.php You can see a sample test page here if that's helpful: http://shuttermuse.com/2013/05/01/photography-quote-of-the-month-2/ Make sure to visiti shuttermuse.com/test first though to get past the coming soon barrier. I want to get the banner to display just above the bit that says "you may also like"
Whistlerdan over 6 years ago
@Whistlerdan: Alright, could you share the code for single.php - perhaps on https://gist.github.com/?
alixaxel over 6 years ago
https://gist.github.com/anonymous/9c84ab38919611a22a15 But placing the banner code in between any of the lines in that single.php file didn't yield the banner in the right place. It always ended up beneath the "you may also like" content, or above the post entirely.
Whistlerdan over 6 years ago
@Whistlerdan: Ok, let me take a look at it.
alixaxel over 6 years ago
Ok thanks man. I guess after all that I'll just use the plugin then as you suggest ! :) Thanks.
Whistlerdan over 6 years ago