By jsimonis on
Nodes on the site have an optional field for a picture. If a picture is uploaded, that image shows at the top of the content with the title overlaid on it. If the picture is not there, the title shows as normal.
I'm trying to use an if statement in my node template (it can be any node type that does this) to set how the content outputs.
The code I am currently using is not working. Even though the node has an image, it is using the "else" statement instead.
Here is the code I am using:
<?php if ($node->field_top_img[0]['view'] !== NULL): ?>
<div id="highlighted"><?php print $node->field_top_img[0]['view']; ?></div>
<h2 class="highlighted"><?php print $title; ?></h2>
<?php else: ?>
<h2 class="not_highlighted"><?php print $title; ?></h2>
<?php endif; ?>
The field's name is field_top_img and it is an image field.
Thanks!
Comments
Use isset
Hi,
Use isset to check if the variable is available or not. If this variable is available then it will print the image if not else part will execute.
Here is the code:
Thanks
I tried that and ended up with the same result.
Did you try it this way?
Still doesn't work
Nope, still nothing. It still acts like there is no image even though there is.
You can see the page here: http://southernsierran.org/about_us/our_mission
This page has an image attached to it via that "Top Image" field. Since I don't really need to print the image via the node template, I removed it from the node template and just moved the image above the body content via the content's display settings. So you can see the image sitting there at the top of the page. However, the "highlighted" css should be applied to the page's title, which is not happening.
Screenshot of content type's fields: http://southernsierran.org/sites/default/files/img_field.JPG
Error...
Also, I get this error:
Notice: Undefined offset: 0 in include() (line 3 of /home/username/southernsierran.org/themes/angeles/node.tpl.php).
Any other ideas?
This is the last remaining thing stopping me from being able to finish up this part of the theme and move on.
Thanks!
Just checking in... I still
Just checking in... I still haven't gotten this to work properly. Is there maybe a step I am missing somewhere? The template file just isn't recognizing anything with regard to that field.
I encountered the same
I encountered the same problem today. I realized I've hidden the field so it always resulted as "empty".
Check /admin/structure/types/manage/[content type]/display to make sure the field isn't hidden.
This might help.