WordPress.org

Ready to get started?Download WordPress

Forums

add content to bottom of single posts using functions.php (18 posts)

  1. Joe Greenwood
    Member
    Posted 3 years ago #

    Hello WP.org world,

    I am trying to add content to the bottom of every post.

    I am using a child theme of Twenty Ten.

    I want to add this content to the bottom of every post using my child theme's functions.php.

    Here is what I have, which sort of works but not really.

    //  add source link to syndicated posts
    
    function tsi_source_link() {
    
     if ( is_single() ) {
       global $post;
       $source_link = get_post_meta ($post->ID, 'syndication_permalink', true);
       if ($source_link) echo '<div id="tsi-source-link" style="text-align:right;"><a href="' . $source_link . '" target="_blank">Read more ...</a></div>';
    }
    }
    add_action('the_content','tsi_source_link');

    Please don't let the overcomplication of the above example confuse you in thinking about my problem. I am using a custom field to attribute the source with its originating permalink.

    Okay so this works, but here is what does not work about it:
    1. adding to 'the_content' puts this content above the post content, not at the bottom where I want it to be. Isn't there some kind of 'after_the_content' hook/tag? How can I get this content to the bottom of the post?

    2. this function makes my Pages not show their content. probably because I am messing 'the_content' when I probably should not be?

    I guess I should use some other function for this. I might simply edit one of the .php files directly. But I figure I'd throw it out here and maybe somebody will help me learn something. Most of my child-theming has been based off Thematic, now I am experimenting with Twenty Ten.

    Thank you all, all the best!
    Joe

  2. danixland
    Member
    Posted 3 years ago #

    May I ask you why are you using functions.php and not working on single.php instead?? If you want to add something under every post single.php is way more straightforward IMHO...

  3. jamespiggot
    Member
    Posted 3 years ago #

    Not sure you can use add_action with the the_content? That is more for a filter where you are checking or amending the content.

    What your code is doing is to replace the_content with the output from your function, you could try something like this (untested code!):

    function tsi_source_link($content) {
     if ( is_single() ) {
       global $post;
       $source_link = get_post_meta ($post->ID, 'syndication_permalink', true);
       if ($source_link) {
       	$content .= '<div id="tsi-source-link" style="text-align:right;"><a href="' . $source_link . '" target="_blank">Read more ...</a></div>';
    }
    }
    return $content;
    }
    add_action('the_content','tsi_source_link');

    But perhaps better to add a wp_footer action then you can echo out your output as in your code above without making any amendments.

  4. Joe Greenwood
    Member
    Posted 3 years ago #

    Fellows, thank you sincerely for the replies.

    @danixland -- Yes, your suggestion makes complete sense. I was avoiding making changes to single.php because I didn't want to mess with the theme core, but since I am using a child theme then I should feel free to go crazy with single.php and the rest. This was sort of a mental concept failure on my part.

    @jamespiggot -- Thank you for clarifying the use of the_content ... and the improved syntax in the snippet. I am curious, if I were to add a wp_footer action, how would I echo the output up above after the post content?

    I think for this specific instance, I will add directly to single.php, as this is a child theme and will be no harm in making this edit.

    Thank you for the replies.
    Joe

  5. jamespiggot
    Member
    Posted 3 years ago #

    Hi Joe,

    Yes, that sounds like a sensible approach, that is the wonderful thing about child themes, you can be much more adventurous in a safer environment.

    The wp_footer action is triggered by a call to the wp_footer() function in the footer.php file of the theme (this is the case for Twenty Ten, may be modified in other themes). It does not do the echo for you, so you have to do this yourself in the function (as you did in your original code sample above). The code example for wp_footer in the Codex is:

    <?php
    add_action('wp_footer', 'your_function');
    
    function your_function() {
      $content = '<p>This is inserted at the bottom</p>';
      echo $content;
    }
    ?>
  6. madpress
    Member
    Posted 3 years ago #

    adding to 'the_content' puts this content above the post content, not at the bottom where I want it to be.

    I'm having the same issue, with Twenty Ten, but also with other themes. However, I see plugins using this action to add a ratings box or the like below the post content, so: any ideas what might be the problem here?

  7. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    @madpress

    what is your funcion code?

    (if it is longer than 10 lines, please use the http://pastebin.com/ )

    are you clear about when to use return and when to use echo ?

  8. madpress
    Member
    Posted 3 years ago #

    Hey alchymyth, basically I wrote something like this:

    function madpress_test() {
    		echo "TEST"; }
    add_filter('the_content', 'add_test');
    function add_test($content) {
    	if (is_single()) {
    		$content .= madpress_test();
    	}
    	return $content;
    }

    This inserts "TEST" between the title (and post metadata) and the post content.

  9. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    two small edits:

    function madpress_test() {
    		return "TEST"; }

    and to show 'test' before the content:

    $content = madpress_test() . $content;
  10. madpress
    Member
    Posted 3 years ago #

    Another valuable lesson! Thanks :)

  11. madpress
    Member
    Posted 3 years ago #

    Whoops, still a bit in the dark here. Is there a way to echo php stuff below the post content, along with the html? For example, the following displays the permalink above the post content, and the text "This is the" below the content.

    function madpress_permalink() {
    		echo the_permalink(); }
    add_filter('the_content', 'add_test');
    function add_test($content) {
    	if (is_single()) {
    		$content .= '<p>This is the ' . madpress_permalink() . '</p>';
    	}
    	return $content;
    }
  12. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    same thing: don't use echo in any functions called by a filter.

  13. madpress
    Member
    Posted 3 years ago #

    Excuse me, but I'm not seeing clear yet. Could you elaborate a little on what should be done to get the above working properly?

  14. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    i missed that you were using the wrong function - the_permalink() automatically echoes the url; get_permalink() needs to be used to return the url -

    function madpress_permalink() {
    		global $post;
                      return get_permalink($post->ID); }

    http://codex.wordpress.org/Function_Reference/get_permalink

  15. madpress
    Member
    Posted 3 years ago #

    That's right! One more question however: is it only possible then to return stuff which you can "get"? What if I would like to display today's date for example?

    <?php echo date('l jS F Y'); ?>

    And on a general note: wouldn't it be nice to have an action hook like loop_end for inserting stuff below the post content?

  16. Mark / t31os
    Moderator
    Posted 3 years ago #

    There's not a get_ equivalent for every function, but quite a few of the Template Tags have them(though not all listed on that link).

    For the date, try.
    http://codex.wordpress.org/Function_Reference/get_the_date

  17. madpress
    Member
    Posted 3 years ago #

    Alas! get_the_date produces the post date, not the current (today's) date.

  18. Mark / t31os
    Moderator
    Posted 3 years ago #

    Yes sorry, read too quickly, you're not referring to the post date, but the date as in now..

    Same applies as with the permalink, simply make sure you return the data, instead of echo, as with the example alchymyth posted.

Topic Closed

This topic has been closed to new replies.

About this Topic