0

Views can be printed in tpl.php files with this code:

<?php print views_embed_view('myview','block_1'); ?>

However, if that view is empty, the view block code will be printed anyway:

<div class="view view-myview view-id-myview view-display-id-block_1 view-dom-id-abc123..."></div>

Is it possible to check if the view has any results first and then print it if it does? E. g.:

<?php if ($view_myview,block_1): ?>
<?php print views_embed_view('myview','block_1'); ?>
<?php endif; ?>

1 Answer 1

3

You can check it using the below code

<?php 
$viewName = 'myview';
$displayid = 'block_1'; 

$view = views_get_view($viewName,$displayid);
$view->execute();
$reviewCount = count($view->result); //It will return number of results
if($reviewCount>0){
print views_embed_view($viewName,$displayid); 
}
?>

Hope it will help.

2
  • It works, thank you. What about performance - does this count slow down the query a lot?
    – take2
    Commented Oct 1, 2014 at 18:23
  • Not that much. But its fine as long as it is working because it is the easiest way to do it.
    – user33576
    Commented Oct 1, 2014 at 18:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.