Home > Back-end >  WordPress Shortcode display Page title & exclude specific words form the Title
WordPress Shortcode display Page title & exclude specific words form the Title

Time:05-05

I have a WordPress Shortcode that displays the page title, but I need now to exclude specific words form the Page Title

For example, sometimes I have in the title words like Best or Top

For example, if the page title is Best City in California shortcode needs to show ( City in California )

This is my code on how to display the title

function post_title_shortcode(){
    return get_the_title();
}
add_shortcode('page_title','post_title_shortcode');

Thank You

CodePudding user response:

It seems easy. Inside the function post_title_shortcode do something similar:

$replacement = [
    'Bay' => '',
    // add as many as you want
];

return str_replace(
    array_keys($replacement),
    array_values($replacement)
    get_the_title()
):

CodePudding user response:

Write some code in your shortcode handler to adjust the text of the title. This kind of thing might work.

function post_title_shortcode(){
    $title = get_the_title();
    $title = trim( str_replace( 'Best ', '', $title, 1 ) );
    $title = trim( str_replace( 'Top ', '', $title, 1 ) );
    return $title;
}
add_shortcode('page_title','post_title_shortcode');
  • Related