0

Is it possible to add an 'if' statement for a content type in a custom views-view.tpl.php?

This is just so I can add text to show if the post in the view is either an 'episode' or an 'announcement'. The content type is called announcement.

I want to do this because I need to identify and style differently based on the post type:

  • If it's from an episode, it displays the episode number.
  • If it's an announcement, it says announcement.

So basically something like so:

<?php if (content_type='whatever') {... code goes here ...} ?>

If this is possible, can you provide an example?

Note: I have a custom theme called views-view-field--first-post--block--created.tpl.php and all it has is <?php print $output; ?>. It's called a field content date.

Also, this is what the output of the entire view looks like: stdClass Object ( [node_created] => 1434783086 [node_title] => CTRL Daily Episode 11 [nid] => 11 [node_type] => ctrl_daily ). [node_type] is what I want.

7
  • yes, but why would you have a function called if()?
    – Geoff
    Commented Jun 30, 2015 at 5:55
  • To identify and style differently based on the post type. If it's from an episode , it displays the episode number. If it's an announcement, it says announcement. <?php if (content_type='whatever' {?>code goes here<?php } ?> Commented Jun 30, 2015 at 5:58
  • oh, an IF statement... you asked about an if function... entirely different things. The answer is still yes
    – Geoff
    Commented Jun 30, 2015 at 6:00
  • Can you give an example? Cannot find help on this topic. And it's for a view. Commented Jun 30, 2015 at 6:01
  • all you asked was whether or not it was possible, and it is. How to implement it really requires more information. Do you want it act on the entire view, on a row, on a field, etc. you need to edit the question and elaborate
    – Geoff
    Commented Jun 30, 2015 at 6:02

3 Answers 3

1

Improving your own code a bit, it'd be better to not set the dummy variable, since you probably won't use it again. To make it a bit cleaner:

<?php if ($row->node_type === 'announcements'): ?>
  <?php // This shows up for node->type of announcement. ?>
<?php else: ?>
  <?php // Everything else. ?>
<?php endif; ?>

Note that it's usually more readable (and thus considered best practice) to use alternative control syntax when in a view.

For additional smart checking, you should probably create a check to make sure that the node_type property is set on the $row object before checking it...

<?php if (isset($row->node_type) && $row->node_type === 'announcements'): ?>
  <?php // This shows up for node->type of announcement. ?>
<?php else: ?>
  <?php // Everything else. ?>
<?php endif; ?>
1

You can access it by adding Node Type field to views and access it using $row variable in views-view.tpl.php.

$rows: The results of the view query, if any

Or $views is also accessible in each and every views template file.

11
  • Don't understand your answer, please be specific. Commented Jun 30, 2015 at 6:48
  • In your template file print_r($row), You will get the fields added in view. So that add a content type, you will get a $row->[content_type] object in template file. Commented Jun 30, 2015 at 6:51
  • I keep getting white screen when I use this code: <?php if($row->[node_type] => 'announcements' ) { ; ?> Show <?php } else { ?> the rest <?php } ?> Commented Jun 30, 2015 at 6:57
  • There might be something fatal error. Try enabling error log and display errors on. Commented Jun 30, 2015 at 6:58
  • Still get white screen. Commented Jun 30, 2015 at 9:06
0

I figured it out. An if statement doesn't work on [node_type] unless it has something to look at. Give it something to compare it will make it work. Also make sure your using row output on themes template in order for the following code to work.

Example: <?php $value = ($row->node_type) ; ?> <?php if($value == 'announcements') { ?> yes <?php } else { ?> nope <?php } ?>

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.