In a template file, how can I use PHP to print some text if a view has a certain value?

The view uses the node ID, so I guess this argument needs to be passed to the view (as it would be if the view was embedded onto the page).

Here is my attempt:

  $view = views_get_view('hide_weight_2');
  if ($view->execute_display('default', $args) == 'Yes' ):
  <h1>Result of view was yes</h1>
<?php endif; ?>

1 Answer 1


If I understand well your question, I guess that you can make this in a rough way from a template View. Inside this template file you can add the respective conditional to show your text.

Here is the part of views-view-unformatted.tpl.php when each result is printed:

<?php foreach ($rows as $id => $row): ?>
  <div class="<?php print $classes[$id]; ?>">   
    <?php print $row; ?>
<?php endforeach; ?>

Then, you can parse $row to catch the respective value and print a message. You can use preg_match() to match the value For example, instead of <?php print $row; ?> you could use:

  if (preg_match("/(here text to match)/i", $row)) {
    print "Aha!:" . $row;
  else {
    print $row;

I guess you can improve this seeking per target fields.

Your Answer

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

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