0

I have a <div> containing a node (a Basic Page), which has a title, body and a header image. I do not want to display the header image inside that div, so I removed it from the block layout. I would like the header image to be in the html before the <div>, that is, outside of the div into which the given content type is outputted (so I cannot solve this via block layout, since the image field is to be outputted outside of the content block).

Can I somehow programmatically instruct my theme to display the header image of my Basic Page? Or is there a better way how to do it?

Example: I need to change this:

<div>
<img src="headerimage.jpg">
<h1>Title</h1>
<p>body</b>
</div>

to this:

<img src="headerimage.jpg">
<div>
<h1>Title</h1>
<p>body</b>
</div>
1
  • 1
    Which Drupal version?
    – Nic
    Commented May 11, 2016 at 11:20

1 Answer 1

0

You can put php code in a block template and write something like:

$node = node_load($node_id);

and then access the specific field you want to show like:

print $node->title;
print $node->field_yourfield["und"][0]["value"]

Edit:

Also, you can modify the node template for the teaser, copying the node.tpl.php file in your subtheme (you can look how to make a subtheme here) and putting the html structure you want inside:

if($teaser)
{
...
}

There are other displays available for nodes other than page and teaser.

5
  • sorry for the basic question... but how do I get the node ID?
    – Ondrej
    Commented May 11, 2016 at 7:24
  • hmmm. Ok, first question is: Are you loading onlyu one node or is this applied to multiple nodes? If you're only loading a node and you know which is it, the node id is the number that appears in the url (like yourpage.com/node/143 ). If you're applying it to multiple nodes, it depopends on how you're getting them. Commented May 11, 2016 at 7:27
  • I'm applying this to multiple nodes. These are normal Basic Pages, I didn't add programming to the way they are aquired, so I assume the standard Drupal logic for displaying content is used. I only changed page.html.twig, which only contains {{ content }} in a div, said simply.
    – Ondrej
    Commented May 11, 2016 at 7:45
  • You can check the node.tpl.php file as suggested. Commented May 11, 2016 at 7:49
  • @Ondrej For Drupal 7 How to get nodeid? For Drupal 8 How to get current node id in Drupal 8?
    – No Sssweat
    Commented May 11, 2016 at 8:23

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.