I am looking to echo the post title at the beginning of each post. I've tried adding the following to functions.php with no luck:

function add_post_content( $content ) {
    if ( ! is_feed() && ! is_home() ) {
        $content .= '<p>.get_post('post_title').</p>';
    return $content;
add_filter('the_content', 'add_post_content');

Should I be trying to update the functions.php file or single.php?


Use the_title():

function add_post_content($content) {

        if(!is_feed() && !is_home()) {

                $content = the_title( '<p>', '</p>', FALSE ) . $content;

        return $content;

add_filter('the_content', 'add_post_content');

The first two arguments are for $vefore and $after. If a post doesn’t have a title, you get not extra markup. The last argument makes the function returning the string. Otherwise it would print it out immediately.

  • Yeah, this echoes at the end of the post. Otherwise, it works great! – Gene Miller Jan 17 '13 at 23:00
  • @GeneMiller See my update. I thought your example code is what you want. – fuxia Jan 17 '13 at 23:06
  • @toshcho, no problem. Your revision is just what I was looking for. Thanks for your help! – Gene Miller Jan 17 '13 at 23:09
  • @toshcho, if I wanted to add some shortcode around that title, would I need to redo the whole function or create two separate functions? – Gene Miller Jan 17 '13 at 23:30
  • @GeneMiller Do not use the shortcode, use the callback directly. – fuxia Jan 17 '13 at 23:32

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.