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

You can check it using the below code

$viewName = 'myview';
$displayid = 'block_1'; 

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

Hope it will help.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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