- This topic has 7 replies, 4 voices, and was last updated 4 years, 9 months ago by
David.
-
AuthorPosts
-
August 2, 2021 at 12:51 pm #1881125
Neil
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!
NeilAugust 2, 2021 at 3:55 pm #1881254Ying
StaffCustomer SupportHi 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
aofin_category( 'a' )withbfor category B.Let me know if it works 🙂
August 2, 2021 at 4:20 pm #1881266Neil
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.
August 2, 2021 at 8:53 pm #1881471Elvin
StaffCustomer SupportHi 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 toin_category( array('a','b') ).If you want to include more, you can do something like
in_category( array('a','b','c','d','e') ).August 3, 2021 at 12:48 am #1881656Neil
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!
NeilAugust 3, 2021 at 3:49 am #1881827David
StaffCustomer SupportHi there,
might be simpler to use a Block Element instead of code:
https://docs.generatepress.com/article/block-element-overview/
You can set the Hook to
after_contentwith a priority of5to position it after the content ( before the post meta ) – see the visual hook guide here:https://docs.generatepress.com/article/hooks-visual-guide/#posts-page
Then set the Display Rule Location to
Post Category > Category AAugust 3, 2021 at 1:45 pm #1882718Neil
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.
August 3, 2021 at 2:13 pm #1882738David
StaffCustomer SupportGood 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. -
AuthorPosts
- You must be logged in to reply to this topic.