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.

Noob question. Is there another way of getting a posts title without using the_title();

I'm asking because I am using a function the take a string parameter, then after some code, returns this string. When I pass in the_title(); as this parameter, for some reason it isn't coming in as a string, therefore, the method fails.

When I pass in "some random string" instead of the_title(); the function works properly.

Make sense?

share|improve this question
1  
This might be the fasted three-answer question in the history of WPSE. :) –  Chip Bennett Apr 10 '12 at 17:04
add comment

1 Answer

up vote 4 down vote accepted

This is because the_title() echos the post title (see the linked documentation). Use get_the_title() instead which returns the title as a string.

Edit

You have two options:

  1. Use get_the_title() to return, rather than echo, the post title
  2. Filter the_title to echo a custom string as the post title

Using get_the_title()

<?php
// NOTE: Inside the Loop,
// or else pass $post->ID
// as a parameter
$post_title = get_the_title();
?>

Using the_title filter

<?php
function wpse48523_filter_the_title( $title ) {
    // Modify or replace $title
    // then return the result
    // For example, to replace,
    // simply return your own value:
    // return 'SOME CUSTOM STRING';
    //
    // Or, you can append the original title
    // return $title . 'SOME CUSTOM STRING'
    //
    // Just be sure to return *something*
    return $title . ' appended string';
}
add_filter( 'the_title', 'wpse48523_filter_the_title' );
?>
share|improve this answer
    
Awesome, exactly what I needed. I understand how it works now. Thanks! –  Romes Apr 10 '12 at 17:00
add 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.