PHP - 将函数添加到字符串

时间:2013-07-02 23:30:20

标签: php function

我对PHP比较陌生,甚至不知道如何提出我需要帮助的问题,所以请原谅我缺乏技术知识 - 或正确地指出这个问题。

我无法找到一种方法将下面的“if(function_exists(”the_ratings“))”代码添加到字符串中,如下面的PHP所示。我知道它下面的方式是不正确的,但我把它放在那里,以显示我需要它显示的方式和位置 - 非常感谢帮助。

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' );

2 个答案:

答案 0 :(得分:1)

使用三元运算符:

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

答案 1 :(得分:0)

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

$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>';
相关问题