0

Could this be converted into an array:

function manipulate_post_title($title){
global $post;
if ($post->ID == 1) {$title = $title.'suffix';}
if ($post->ID == 2) {$title = $title.'different_suffix';}
}
add_filter ('the_title','manipulate_post_title',10,1);

Something like:

'1' => $title.'suffix',
'2' => $title.'different_suffix'

New version based on your code (doesn't work):

function manipulate_post_title($title){
global $post;
$title = get_the_title();

$title_array = array(
'1' => $title.'suffix',
'2' => 'prefix'.$title,
'3' => 'completely different title'
);

  if ( is_single() && isset($title_array[$post->ID]) ) {
    $title = $title_array[$post->ID];
  }

  return $title; // Don't forget the return value!


}
add_filter ('the_title','manipulate_post_title',10,1);
2
  • How do you define the title_array values? Are they hardcoded in some file / do you get them from some database field / something else? Also you mention that you want to change post title without logging in -> do you want to save the new title to database?
    – Philipp
    Jul 5, 2014 at 14:10
  • I'll define the values inside the functions.php file. I don't want to save anything to the DB - just dynamically re-generate the titles - the point is to make it easier to experiment with titles without any extra hassle like updating the DB or logging in to the admin area. The blog only has about 30 posts so dynamically generating the titles shouldn't be an issue resource-wise.
    – kat
    Jul 5, 2014 at 15:56

1 Answer 1

1

Your example code could easily be converted to an array, like this:

Updated and improved code for your new example:

function manipulate_post_title( $title, $post_id ) {
    $title_array = array(
        '1153' => $title . '-suffix',
        '2' => 'prefix-' . $title,
        '3' => 'completely different title',
    );

    if ( is_single() && isset( $title_array[ $post_id ] ) ) {
        $title = $title_array[ $post_id ];
    } else {
        // For debugging I set the title to "[123] Title" so I can find the post_id
        $title = '[' . $post_id . '] ' . $title; 
    }

    return $title;
}
add_filter( 'the_title', 'manipulate_post_title', 99, 2 );

Your code does not work because it has an infinite loop: You call the function "the_title()" inside the filter "the_title" (this will call the filter again and again until a memory error happens)

The code above works on my installation and will change the title as it is returned by the WordPress function "get_the_title()" or "the_title()" In the Twentyfourteen theme it will change the title that is displayed inside the <h1> tag of the page, but not the <title> element that is displayed in the browser tab!

Example screenshot from my test-page

3
  • I'm trying to create a function to change/edit the posts' title without having to login to Wordpress. Please see the new version above.
    – kat
    Jul 5, 2014 at 13:44
  • The updated code seems to be working perfectly! I'll do some more testing and will get back ASAP!
    – kat
    Jul 5, 2014 at 16:00
  • 1
    Confirmed - it works exactly as I had in mind! Thank you so much!
    – kat
    Jul 5, 2014 at 16:13

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.