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!