Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
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. –  toscho 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. –  toscho Jan 17 '13 at 23:32
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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