2013-07-02 74 views
1

我對PHP比較陌生,甚至不知道如何提出需要幫助的問題,所以請原諒我缺乏技術知識 - 或者正確地提及術語這件事。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'); 

回答

0

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

工作,謝謝!我有一個問題,根據http://maverick.n8geeks.com/評級現在被添加到頁面的頂部 - 這是一個PHP問題,或者我進入插件領域嗎? –

+0

順便說一下,它沒有與if語法一起工作。 –

+0

從第二個代碼塊中刪除一個墳墓('),應該修復它。我不確定你正在使用什麼框架,但我猜如果它顯示在頁面的頂部,也許the_title()實際上是回顯結果而不是返回它(即該行將標題立即放入HTML將其附加到$ content變量)。 – cryocide

1

使用三元運算符:如果你想這樣做內聯,你可以使用三元運算

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

如果你想使用if語法

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

我只是加了括號,因爲如果函數存在,div不會被添加 – bwoebi

+0

@bwoebi:是的,thx,應該可以工作。 – elclanrs

+0

謝謝elclanrs。這與其他答案一樣有效 - 當我問另一個人時,我想知道是否有任何理由將評級張貼到頁面頂部,如http://maverick.n8geeks.com。這恰好是在使用時,例如the_title不是get_the_title。 –