skip to Main Content

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

2

Answers


  1. 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');
    
    Login or Signup to reply.
  2. It seems easy. Inside the function post_title_shortcode do something similar:

    function post_title_shortcode() {
        $replacement = [
            'Bay' => '',
            // add as many as you want
        ];
    
        return str_replace(
            array_keys($replacement),
            array_values($replacement),
            get_the_title()
        );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search