0

I'm looking to add a <div> with some more html content to the bottom of specific posts in the loop. The the_post action can be used to add the <div> to the beginning of the post in the loop, but I'm hoping to add it to the end.

EDIT The closest things I've been able to do is to add a classname to the desired post in the loop using post_class. Then I used jQuery(.classname).append('<div>custom content</div>) to add the custom content. The problem is, mixing jQuery and php in this manner seems to be less than optimal because I want to pass php info to the custom content...

3

It sounds like you want a filter on the_content.

function add_content_wpse_97277($content) {
  global $post;
  if ($post->ID == 123) {
    $content .= 'additional content';
  }
  return $content;
}
add_filter('the_content','add_content_wpse_97277');

I don't know what conditions you need though. You did not explain that in the question. The above should match a post with an ID of "123".

For index pages you may need to add the same callback to the the_excerpt hook. You can control where the function adds content with template tags like is_home or is_single.

Honestly, this is how you should be adding your content both above and below the post. Echoing content from an action like the_post can work but is also a good way to get yourself in trouble. In this case I'd say it is particularly dangerous. the_post runs inside setup_postdata which does frequently run inside a loop that echoes content immediately, but if that function-- setup_postdata-- runs inside a Loop that doesn't echo immediately, for example to build a string, your markup will make a mess. There is also loop_start and loop_end but I don't think those are what you want.

Having seen your question and your comments and considered them, I have to say that your basic approach is wrong. You should be using the the_content and the the_excerpt, and/or possibly the get_the_excerpt filter as well.

Unfortunately filtering the content/excerpt doesn't do what I need it to do. There are often divs or footers added to a post (in the loop) below the excerpt/content. That means filtering the content/excerpt would not make my content appear at the bottom of the post in the loop. Thanks though, your help is appreciated!

quoted from a comment below

I doubt there is a reliable way to do what you want. The only filter you have to work with that I can see is the_post. You could use that to insert between posts by tracking which post of the Loop you are on and inserting content conditionally. That is:

  1. If post number 0, then insert the begin <div>
  2. If post number 1, then insert </div> and <div>
  3. ...
  4. If last post, then insert </div>

But again, you are going to make a mess echoing content from the the_post filter when setup_postdata is used in a context that does not echo content immediately-- for example, in a context where a string is being built and returned.

  • Sorry but as far as I can tell that doesn't add a <div> to the loop. This adds content to the body of post 123's content. I'm looking to add a div below post 123 in the loop, not on post 123's single page. Sorry if that wasn't clear and thanks for the response! – bob_hardy Apr 25 '13 at 23:44
  • This filter is not specific to the single post display unless your theme is intentionally written that way. There is a pretty fine distinction between "appended to the post body" and "after the post in the Loop". The answer is going is going to depend on your Loop and may require editing that Loop. There is no hook that I can think of that does what it now sounds like you want. Please edit your question to include the Loop code. – s_ha_dum Apr 26 '13 at 0:12
  • It's for a plugin, so I won't be able to edit the loop code... unless there's a way to do that that I don't know. – bob_hardy Apr 26 '13 at 2:35
  • @bob_hardy, see the edit – s_ha_dum Apr 26 '13 at 4:12
  • Thanks for your help. Unfortunately filtering the content/excerpt doesn't do what I need it to do. There are often divs or footers added to a post (in the loop) below the excerpt/content. That means filtering the content/excerpt would not make my content appear at the bottom of the post in the loop. Thanks though, your help is appreciated! – bob_hardy Apr 26 '13 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.