I need to show the title of the current post in a particular way.
Post titles are like:
Balubano 24
Balubano 25
Balubano 26
etc...
I want to only show:
Bal. 24
Bal. 25
Bal. 26
In summary, just replace the characters ubano
with a dot
. Balubano 176 becomes Bal. 176
Note: I don't want to edit the database, just change how it appears in the template when I call it.
How can I edit the following in order to achieve my goal?
<?php echo get_the_title(); ?>
You can alter the way titles are displayed by using a filter.
The function you're using, get_the_title()
, is run through the the_title
filter.
To modify the output add the following code to your functions.php file:
/**
* Abbreviate 'Balubano' in post titles.
*
* @param string $title Post title.
* @return string
*/
function wpse_filter_post_titles( $title ) {
return str_replace( 'Balubano', 'Bal.', $title );
}
add_filter( 'the_title', 'wpse_filter_post_titles' );
You can also shorten echo get_the_title()
to just the_title()
.
If I understand correctly, you are trying to add abbreviations in the post
title.
If the abbreviation list is known, you can use the str_replace function as such:
<?php
$abbr=array(
'Balubano'=>'Bal.',
'Street'=>'st.',
'Avenue'=>'av.',
);
echo str_replace(array_keys($abbr),$abbr,get_the_title());
?>