Search code examples
phpwordpresssubstr

Verify if WordPress title starts with character


I am trying to verify if the WordPress post title starts with certain 3 first characters to achieve a condition. Here is what i tried by it brings back the title itself.

$verify = the_title();
if(substr($verify,0,3) == '000'){
$maptitle = 'test 1';
} else {
$maptitle = 'none';}

Solution

  • You should use get_the_title(). the_title() will print the title to the screen. In your case, you need to return the title so that you can parse it.

    $verify = get_the_title();

    https://developer.wordpress.org/reference/functions/get_the_title/

    If you want to use the_title(), you can, but you will need to specify that you want the value returned.

    $verify = the_title('', '', false);

    https://developer.wordpress.org/reference/functions/get_the_title/