Sometimes you want to include a short note under all posts in your blog - without installing an additional plugin. For me, the rule applies: What you can do without a plugin, you should do without, because every plugin inflates Wordpress again and often the installed plugins have a significant overhead of functions that you actually don't need at all. Another point that speaks against more and more plugins is of course the security of WordPress. Most hacker attacks run through outdated or insecurely programmed Wordpress plugins. The WordPress core is usually very secure if you take care of the updates.
Chapter in this post:
Solution via functions.php
I wanted to add an addendum to the articles in my blog that indicates that I participate in the Amazon affiliate program. According to Amazon's new regulation in 2018, the sentence "As an Amazon partner, I earn from qualifying purchases." be set up on every page where Amazon advertising is found. Since I didn't want to edit all these pages individually, I'll just add the note under each post.
In order to add such small functions, Wordpress offers the possibility to include the corresponding PHP code snippets in the “functions.php” file in the theme folder. If you don't have this file yet, it's best to create it and copy the code below into it.
The “the_content” hook makes it possible
With "Hooks", Wordpress offers flexible options for expanding or changing the functions that run in Wordpress. We now use the the_content hook to modify the content of the post or page before it is rendered to the webpage. So that the addition is only installed on pages and posts and is not output in category overviews or the like, there are still queries in_the_loop()
and is_main_query()
for use. Here is the finished script:
add_filter ('the_content', 'filter_the_content_in_the_main_loop'); function filter_the_content_in_the_main_loop ($ content) {// Check whether we are in the loop of a post or a page if ((is_single () OR is_page ()) && in_the_loop () && is_main_query ()) {// The HTML part for the font you can change or expand as you like return $ content. ' Note: As an Amazon partner, I earn from qualified purchases. '; } return $ content; }
If you only want the reference to articles and not to pages (such as imprint or similar), then change the if statement as follows:
if ( is_single() && in_the_loop() && is_main_query() ) {
If you have any questions about the usage or suggestions for improving the code, I look forward to your comment.
Related Articles
Jens has been running the blog since 2012. He acts as Sir Apfelot for his readers and helps them with technical problems. In his spare time he rides electric unicycles, takes photos (preferably with the iPhone, of course), climbs around in the Hessian mountains or hikes with the family. His articles deal with Apple products, news from the world of drones or solutions to current bugs.
Thank you for your contribution - that's exactly what I was looking for.
Unfortunately it doesn't work for me.
But I also have little idea about the subject.
If I compare it with other function calls, they still have an add_action () or something similar with them.
If you have a tip, I would be happy to hear from you.
Thank-you! :-)
Frank
Hey Frank! 1000 thanks for your note. The “add_filter()” call was indeed missing. Without the the function will not be built into the hook. I added it. Please try if it works now with the new code! LG! Jens
Works like a dream - thank you very much! :-)
Hello Frank,
thank you very much for your contribution! That's exactly what I was looking for :)
I still have one question: Is there a way to read the focus keyword from Yoast and insert it into the text above?
Eg note: As an Amazon partner, I earn money from qualified purchases from [Focus keyword] (e.g. Schaltraum).
Best regards,
Sandra
Hi Sandra! I think all the variables and also the focus keyword can only be used in the snippet. So in meta title and meta description. With PHP programming you might be able to extract it and somehow put it in the sentence, but I have no idea how to do that.
Hello Jens, no problem! Then I'll paraphrase it first :)
Anyway, thank you very, very much!
Best regards,
Sandra
Hi Jens,
a question about this PHP snippet. When I insert this code, a text is displayed under posts (so far so good), but also under Woocommerce product pages. This text shouldn't actually be there. How do I have to adapt the code. Thank you very much.
gruß
Norbert
Hello Norbert! Good question, next question! :D I have no clue about WooCommerce. I suppose you would have to add a query like “if (is_page())” or something, but with detection if it's a WooCommerce product page. But unfortunately I don't know how that works because I don't know the WooCommerce peculiarities... :(
Hi,
Is it possible to create a custom field with ACF Pro and have it automatically displayed for all amounts, ideally directly under the h1 heading? If so, is there a plugin for that?
Hello Christof! I don't think ACF is that well suited for this purpose. The fields are always related to the corresponding post, if I'm not mistaken. But you can probably do it with Ad Inserter solve. There you can create a text and then tell the field where it should appear. There is also the option “all posts” and I think you can also define “under the headline” or something…
Hi Jens,
Thanks for the reply.
I just wanted the prices that I added at ACF to appear depending on the post. Since there are different prices, which is why I use ACF. Can you combine that with Ad inserter? It should look something like the Reiseuhu website, as well as the different prices that appear there under the H1.
You can also insert PHP code in one place in Ad Inserter. If you manage to read and output the ACF database content, you have solved the problem. : D
Hi Jens,
Thanks, it worked really well, do you remember how the prices there can adjust the size and color?
Hello Christof! You will have to do this with CSS. Or you build it directly into an HTNL tag with style=”xyz”.
Hi Jens,
Thank you for your help. Really helped, thanks! :)
lg
christoph
Great, please! It was just wishy-washy help, but sometimes you just need the right push. 😂
Hallo,
the code doesn't work for me..
my functions.php looks like this:
is_home ) {
$query->set( 'cat', '-390' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
// Add short code
function my_backlink() {
return 'Back to overview';
}
add_shortcode( 'backlink', 'my_backlink' );
add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );
function filter_the_content_in_the_main_loop( $content ) {
// Check if we are in the loop of a post or page
if (( is_single() && in_the_loop() && is_main_query() ) {
// You can change or expand the HTML part for the font as you like
return $content . ”;
}
return $content;
}
Hello Christian! Basically, when you're reporting bugs to people and looking for help, it would be nice to have nothing more than "works for me" as input. Do you mean there is no output? Do you see an error message? For example, I see that you have deleted the text that is to be output. So simply no output can come. But maybe you give a little more feedback where it's stuck. Thanks!
And I think you have to replace all the single quotes in the code with new ones. They don't look like the ones you get with SHIFT + # to me. But it could be that Wordpress is already displaying it incorrectly in my code.
I just wanted to say a quick “thank you”, this is exactly what I was looking for. Even I, as a layman, got it implemented straight away.
Thank you :)
Hello Mike! You're welcome. 😊