0

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?

1 Answer 1

1

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.

6
  • Yeah, this echoes at the end of the post. Otherwise, it works great! Jan 17, 2013 at 23:00
  • @GeneMiller See my update. I thought your example code is what you want.
    – fuxia
    Jan 17, 2013 at 23:06
  • @toshcho, no problem. Your revision is just what I was looking for. Thanks for your help! Jan 17, 2013 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? Jan 17, 2013 at 23:30
  • @GeneMiller Do not use the shortcode, use the callback directly.
    – fuxia
    Jan 17, 2013 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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