WordPress.org

Ready to get started?Download WordPress

Forums

How to add something after the post? (7 posts)

  1. troyward
    Member
    Posted 1 year ago #

    I am trying to display terms of my custom taxonomy below the posts. I can add

    <?php the_terms( $post->ID, 'people', 'People: ', ', ', ' ' ); ?>

    But I want to add it to my child theme's functions.php file so that no matter which theme I use the terms get displayed below the post.

    I know about action hooks and I think I can hook this with the_content I tried this code

    function my_terms($content) {
    	$terms .= the_terms( $post->ID, 'people', 'People; ', ', ', ' ' ); ;
    	return $content.$terms;
    }
    add_action('the_content', 'my_terms');

    It works but it adds terms between post content and title. I would like the terms to appear after the content. Can someone please tell me how I can do this?

    Thanks

  2. gidd
    Member
    Posted 1 year ago #

    You should use add_filter instead of add_action.
    Here is an example: http://www.wprecipes.com/wordpress-tip-insert-custom-content-after-each-post

  3. troyward
    Member
    Posted 1 year ago #

    gidd thanks for the tip but even with add_filter the custom content appears before the post between post content and post title.

    I tested it on twenty twelve and twenty eleven. same result. Here is the code I tried:

    function add_post_content($content) {
    	if(!is_feed() && !is_home()) {
    		$content .= '<p>' . the_terms( $post->ID, 'people', 'People: ', ', ', ' ' ) . '</p>';
    	}
    	return $content;
    }
    add_filter('the_content', 'add_post_content');
  4. jimihenrik
    Member
    Posted 1 year ago #

    I think your problem lies within 'People: ', ', ', ' ' )

    Something like 'People: ', '', '', '') or just 'People: ') would be correct.

    You know, as in 'something here', 'or', 'something', 'else' and for now you only have the "opening quote" for all of those.

  5. troyward
    Member
    Posted 1 year ago #

    jimhenrik thanks for the tip, removed there and used 'People: ') still the same result. I also noticed that the filter strips out HTML <p> and </p>

    It just shows People: Bob, Karen right above the post with no <p> surrounding them.

  6. jimihenrik
    Member
    Posted 1 year ago #

    I have to apologize first and admit, that I have not worked with any of the filters, ever.

    Still, that's quite strange. With a quick googling that should be the right code.

    function your_content($content){
      if (is_single()) {
        $content .= '<p>Your Content Here</p>';
      }
      return $content;
    }
    add_filter( "the_content", "your_content" );
  7. troyward
    Member
    Posted 1 year ago #

    I got replacing the < with &lt and &gt but it still strips them out. Also terms still appear below the title instead of below the post content.

Topic Closed

This topic has been closed to new replies.

About this Topic