1

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 Answers 2

1

Use a ternary operator:

$content .= '<div class="ratings">'. ( function_exists("the_ratings") ? the_ratings() : '' ) .'</div>';
Sign up to request clarification or add additional context in comments.

2 Comments

I just added parenthesis as else the div won't be added if the function exists
Thanks elclanrs. This works as does the other answer - as i asked the other guy, i was wondering if there is any reason the ratings are posted to the top of the page as per maverick.n8geeks.com. This happens to be when using, for example the_title isntead of get_the_title.
0

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

3 Comments

That works, thanks! One question i have though, as per maverick.n8geeks.com the ratings are now being added to the top of the page - is this a PHP issue or am i getting in to plugin territory here?
Removed a grave (`) from the second code block, that should fix it. I'm not sure what framework you're using but I would guess if it shows at the top of the page maybe the_title() is actually echoing the result instead of returning it (i.e. that line would put the title into the HTML immediately instead of appending it to the $content variable).
Actually checked the WP Post Ratings plugin files and replaced the first 3 echo's for returns and it seems to work exactly as i need. Thanks a lot buddy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.