PHP-向字符串添加函数


PHP - adding function to string

我对PHP还比较陌生,甚至不知道如何提出需要帮助的问题,所以请原谅我缺乏技术知识,或者正确使用术语。

我无法像下面的PHP中那样,找到将下面的"if(function_exists("the_ratings")"代码添加到字符串中的方法。我知道下面的方式是不正确的,但我把它放在那里是为了展示我需要它如何以及在哪里展示——非常感谢帮助。

function latestreleases()   {
$args = array( 'posts_per_page' => 30, 'cat' => '-1, -2' );                  
$last_5_posts_query = new WP_Query( $args );
while($last_5_posts_query->have_posts()) : 
    $last_5_posts_query->the_post();
    $link = get_permalink();
    $title = get_the_title();
    $description = excerpt(16);
    $details = 'Watch ';
    $readmore = 'Read more...';                  
    $content .= '<div class="all-latest-movies">';
    $content .= '<h3><a href='.$link.' target="_top">'.$title.'</a></h3>';
    $content .= '<div class="thumbnail"><a href=" '.$link. ' ">'
    . get_the_post_thumbnail( null, "home-movie-thumbnail") 
    . '</a></div>';
    $content .= '<div class="description">' .$description. '&nbsp;<a href= '.$link.' >' .$readmore. '</div>';
    $content .= '<div class="view-listing"><a href= '.$link.' target="_blank" >' .$details. $title. '</a></div>';
    $content .= '<div class="ratings">' if(function_exists("the_ratings")) { the_ratings(); } '</div>';
    $content .= '</div><!-- .fetured-movies -->';
endwhile;
return $content;
}
add_shortcode('LatestReleases', 'latestreleases' );

使用三元运算符:

$content .= '<div class="ratings">'. ( function_exists("the_ratings") ? the_ratings() : '' ) .'</div>';

如果要内联,可以使用三元运算符

$content .= '<div class="ratings">'.(function_exists("the_ratings")?the_ratings():'').'</div>';

如果要使用if语法

$content .= '<div class="ratings">';
if(function_exists("the_ratings")){
 $content.=the_ratings();
}
$content.='</div>';