WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Adding custom text to bottom of all posts in particular category (8 posts)

  1. ChrisStoneman
    Member
    Posted 2 years ago #

    I'd like to add the same piece of custom text to the bottom of every post in a certain category. How do I do this?

    I've tried using several plugins, 'PostEnder' almost works but doesn't display the text on the homepage. 'AddPostURL' and 'AddPostFooter' add the text to all posts.

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with something like this in your theme's functions.php [untested]:

    add_filter('the_content', 'custom_category_text');
    
    function custom_category_text($content){
    global $post;
    $custom_category_text = '<p>Put your Custom category text here.</p>';
    if(in_category('3')){ // change catetegory ID 3
    	$content =  $content . $custom_category_text;
    }
    return $content;
    }

    Change the category ID in in_category('3') to the correct category ID.

  3. ChrisStoneman
    Member
    Posted 2 years ago #

    This has worked beautifully, THANK YOU!

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    You're welcome. Glad you got it resolved.

  5. gocreations
    Member
    Posted 1 year ago #

    Hi...this code working for me also...
    Does anybody knows how i can add some diferent content in two diferent categories.
    ex. i want to add "My name" at category "products"
    and i want to add "website name " at category "sales"
    Thanks..

  6. bennygill
    Member
    Posted 1 year ago #

    Hello,

    I want to use this too! But I'm having trouble because I want the 'text' to include some code (a few div's). It works great when its only text, but when I try to use a div I get an error.

    Here is what I'm using:

    add_filter('the_content', 'custom_category_text');
    
    function custom_category_text($content){
    global $post;
    $custom_category_text = '<div class="alert alert-block">This is some text inside a DIV tag</div>';
    if(in_category('3')){ // change catetegory ID 3
    	$content =  $content . $custom_category_text;
    }
    return $content;
    }

    Any help very much appreciated!

    Thanks,
    Ben

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    @bennygill
    Your code seems to work for me. What is the error you get?

  8. bennygill
    Member
    Posted 1 year ago #

    I figured out what the problem was.

    I had apostrophes as part of the text in the code ---> '

    Once I removed those it worked fine.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.