Wordpress php, get_the_title() coming out empty wh

2019-08-21 17:27发布

问题:

I'm currently working on a Wordpress site. And what the client wants is to be able to create a schedule for a convention. What he is asking for is the option of clicking a button in each post which would add a 15 minute Q&A slot.

The way I went about the time was to use the title of a post which would be written as 11:20 AM - 11:40 AM

the problem I'm having is that the get_the_title() function comes out empty when used in a substr & strrchr. The interesting thing is when I put it as "11:20 AM - 11:40 AM" instead of get_the_title() it comes up fine.

By the way I'm still pretty new with php so Im probably not going about it the right way.

my code

echo qatime(get_the_title(), "AM");

and the code in the functions.php file is

function qatime($string, $cat){
$newCat = $cat;
$stringsplit = substr(strrchr($string, "-"), 1);
$pieces = explode(":", $stringsplit);
$firstnum = preg_replace("/[^0-9]/", '', $pieces[0]);
$lastnum = preg_replace("/[^0-9]/", '', $pieces[1]);
$qaTime = 15;
$minute = $lastnum + $qaTime;

if($minute < 60){
} else {
    $firstnum += 1;
    $minute -= 60;
}
if($firstnum >= 12){
    $firstnum -= 12;
    if($firstnum == 0){
        $firstnum = 12; 
    }
    $newCat = 'PM';
}
if($pieces[0] == 12){
    $cat = 'PM';    
}
$minutes = str_pad($minute, 2, "0", STR_PAD_LEFT);
return $stringsplit." ".$cat." - ".$firstnum.":".$minutes." ".$newCat;
}

Solved

Thanks for the quick responses. I found that instead of using get_the_title(), $post->post-title made it work.

 $string = $post->post_title;
 echo qatime($string,'AM');