Site logo

Archived topic

Add text at bottom of all posts in a category

7 replies · Started by Neil on August 2, 2021

Viewing posts 1–8 of 8

I found this example code which is working with GeneratePress:

add_filter('the_content','add_my_content');
function add_my_content($content) {
$my_custom_text = '<h2>Test</h2><p><font size="2">Some test text is here.</font></p>'; //
if(is_single() && !is_home()) {
$content .= $my_custom_text;
}
return $content;
}

That adds the same text to the bottom of all posts.

I'd like to be able to to add text to the bottom of all posts in particular categories.
For example...
All posts in Category A have the text 'This is Category A' at the bottom.
All posts in Category B have the text 'Category B post' at the bottom.

Could you tell me how to do that please?

Thanks very much for your help!
Neil

Hi Neil,

Give this snippet a try:

add_filter('the_content','add_my_content');
function add_my_content($content) {
$my_custom_text = '<h2>Test</h2><p><font size="2">This is Category A.</font></p>'; //
if(is_single() && !is_home() && in_category( 'a' ) ) {
$content .= $my_custom_text;
}
return $content;
}

Replace the a of in_category( 'a' )with b for category B.

Let me know if it works :)

Thanks very much it's working! :-)

I have a follow-up question (sorry I never really do any coding)... How do I add the text for Category B also?

I mean when I use your example I get the text 'This is Category A.' at the bottom of my Category A posts.
I'd like ‘Category B post’ to be shown at the bottom of Category B posts as well.

Sorry I tried adding some variations to your example but couldn't get it to work.

Hi Neil,

If you need to add more categories, go to this line: if(is_single() && !is_home() && in_category( 'a' ) )

Modify the in_category( 'a' ) part to in_category( array('a','b') ).

If you want to include more, you can do something like in_category( array('a','b','c','d','e') ).

Thanks.
I think that will add the same text to multiple categories, right?
I want to add different text to different categories.

‘This is Category A' at the bottom of Category A.
‘Category B post’ at the bottom of Category B.

Could you tell me how to do that please?

Thanks!
Neil

Thanks David. I somehow ended up with my sidebar below my main text when I tried that.

I managed to figure it out (I know this is totally obvious for anybody with more than 30 second's coding experience):

add_filter('the_content','add_my_content');
function add_my_content($content) {
$my_custom_textA = '<h2>Test</h2><p><font size="2">This is Category A.</font></p>'; //
$my_custom_textB = '<h2>Test</h2><p><font size="2">This is Category B.</font></p>'; //
if(is_single() && !is_home() && in_category( 'A' ) )
{$content .= $my_custom_textA;}
elseif(is_single() && !is_home() && in_category( 'B' ) )
{$content .= $my_custom_textB;}
return $content;
}

Thanks very much for your help, the support section here is awesome. I've found the answer for every other question I had in the archive.

Good solution.
That issue with the Block Element, would generally mean there was some broken HTML in the content.
Not sure if you added any similar to the function your using. The issue could be related to the <font> element your using, its been deprecated and not recommended for use on modern browsers:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/font

Although it may still work, it could be a problem.
Would be better to give the element a CSS Class and set the size with the font-size property.

This archived topic is closed to new replies.