关闭
douchan6512 2013-07-02 15:30
浏览 25
已采纳

PHP - 将函数添加到字符串

I'm relatively new to PHP and don't even really know how to ask the question for which i need help with, so please excuse my lack of technical knowledge - or referring to terms correctly for that matter.

I cannot figure out a way to add the "if(function_exists("the_ratings"))" code below to a string as in the PHP i have below. I know that the way it is below is not correct, but i've placed it there to show how and where i need it to display - help is greatly appreciated.

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条回答 默认 最新

  • dshqd84261 2013-07-02 15:36
    关注

    If you want to do it inline you can use the ternary operator

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

    If you want to use the if syntax

    $content .= '<div class="ratings">';
    if(function_exists("the_ratings")){
     $content.=the_ratings();
    }
    $content.='</div>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部