Nikki Mather
Nikki Mather

Reputation: 1148

PHP - adding function to string

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

Upvotes: 1

Views: 95

Answers (2)

cryocide
cryocide

Reputation: 731

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

Upvotes: 0

elclanrs
elclanrs

Reputation: 94101

Use a ternary operator:

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

Upvotes: 1

Related Questions