Site logo

[Resolved] Add text at bottom of all posts in a category

Home Forums Support [Resolved] Add text at bottom of all posts in a category

Home Forums Support Add text at bottom of all posts in a category

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #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!
    Neil

    #1881254
    Ying
    Staff
    Customer Support

    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 🙂

    #1881266
    Neil

    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.

    #1881471
    Elvin
    Staff
    Customer Support

    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') ).

    #1881656
    Neil

    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

    #1881827
    David
    Staff
    Customer Support

    Hi 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_content with a priority of 5 to 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 A

    #1882718
    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.

    #1882738
    David
    Staff
    Customer Support

    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.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.