How to show the image of custom content type in drupal page.tpl.php? - drupal

I just created a custom content type in drupal7 and I want to manage the image of that content in my template. <?php print $content; ?> displays all the field of that content. But I just want to show the image only.How can I do so?

You must somehow get node id of node that contains that picture (hard-code it or something). Then use node_load() function to load whole node - image field will be one of available fields in loaded node object. Use print_r or something to check out available fields, so you can construct exact code, but it should be something like this:
$node = node_load($nid);
$picture_uri = $node->field_myimagefield['und'][0]['uri'];
// To get image path in specific image style you created:
$picture_style_path = image_style_url('style_name', picture_uri);
// To get original image path
$picture_original_path = file_create_url($picture_uri);
Then you can directly print picture path into img html tag...
But, are you sure that you are using right template file? If you want template just for your content type you should use node, not page template (which is wrapper for node template).

Related

How do I display either body or file from content

I am a new in Drupal. I want your help for doing this please help
I have created one Content type with fields( Title, Body, File) and also added content through this content type. But Some of the content didn't have file filed and some of the content having the only file. In the view, I want to show both either body or file. if suppose file field is empty then it should display the body of the same content.
Thanks.
You could do this by creating a custom node template for particular content type. So if you have a content type 'news', for example, the you can create a node template for this content type and customized the front-end display of the content. See below how it would work.
As per the Drupal theme suggestions, create a node template, named, node--news.tpl.php (copying the existing node.tpl.php file) in your custom (active) theme directory under the templates folder. On this template, you will be able to access the $node object variable, containing complete node information including fields and data.
Suppose your fields are field_image and body then replace your template's code print render($content); with the following lines:
if(!empty($content['body']){
print render($content['body']);
} else {
print render($content['field_image']);
}
For more details: https://www.drupal.org/node/1323842
Hope this help!

Creating a link field in a custom content type

I have several custom content types and in one specific one I need to offer two fields, one for the href for a link, and one for the text of a link this way I can build and style it with minimal user involvement of the HTML/CSS. I also have a custom node.tpl for this content type. My problem is that drupal throws divs around each field I create that aren't in my template file for this content type (node-custom.tpl) and I can't put the href for a link with divs around it inside google.co.uk</div>"> See my problem. Maybe I'm doing this all wrong so any other ideas are welcome.
Please note I'm trying to create this site with minimum involvement of HTML/CSS access for the user. I'm aware I could hand code the link in the field.
The easiest way to do this would be to use a preprocess function in your template.php file and build the link up manually:
function mytheme_preprocess_node(&$vars) {
$node = $vars['node'];
if ($node->type = 'my_type') {
$uri = $node->field_name_of_link_field[LANGUAGE_NONE][0]['value'];
$text = $node->field_name_of_display_text_field[LANGUAGE_NONE][0]['value'];
$vars['my_link'] = l($text, $uri); // Using Drupal's l() function to render a link
}
}
Then in your node template file you'll have access to the variable $my_link which you can output anywhere, and will contain the correct HTML for the link. Finally, go to the "Manage Display" page for your content type and set the display of the two fields you no longer need to output to 'Hidden'.
There are other ways so if that's no good let me know
EDIT
Just to add, I think the easiest way to do this would actually be to install the Link module and use the provided field type instead of the two other fields you're currently using.

new content type, with completely blank(body of node only) output theme files

I have a content type that is only used for scripts on my drupal site that should return json data. So the issue then becomes that I can't have any of the theming elements of the site in the output.
So I know I need blank (only the output variable) tpl files for the following:
html.tpl.php
page.tpl.php
region.tpl.php
block.tpl.php
field.tpl.php (the manual says this isn't used but its the only way I could find to remove field divs around the body of my page)
So my question is, how can I create all of the content specific files for this content type? I know its easy to do a theme override in template.php BUT I can only get it to work for html.tpl,page.tpl and thats it. Drupal seems to ignore any content specific functions from region down to field.
I noticed you tagged this drupal-7, which I haven't tried yet. But, in Drupal 6 I would accomplish this by adding a new page template to $vars['template_files'][] in the page preprocess function of template.php. Example:
$node_type = $vars['node']->type;
if ($node_type == '{your content type name here}') {
$vars['template_files'][] = "page-" . $node_type;
}
If your content type was called 'scripts', then Drupal will look for a page called page-scripts.tpl.php, which will contain only one line:
<?php print $content; ?>

Drupal 6: print all body field content to node template file

I tried to display body content with:
<?php print $node->content['body']['#value']; ?>
However, it doesn't display all body content, it just display first paragraph of body content, sometimes 2 paragraph if it is short :/
I need to print all body. how can I do that?
Thanks a lot! Appreciate helps!
The shortened body content hints on it being filled/rendered for 'teaser' view instead of 'full'. In what context do you issue this print statement?
EDIT: The node templates are usually used for both, teaser and full output, but the decision on what to use, as well as the population of the content entries in the node object happen outside of the node template files. Within the node template file, the variable $teaser will be TRUE, if the node is to be shown as a teaser.
So you need to check in what context your node template gets called, as you'll have to configure that context to render the node as 'full'. This could be in many places, depending on who is responsible to provide the nodes you want to theme, e.g. if the node template gets called from a view, you'll need to configure the view to use 'full page' output, if it comes from a module, you'll need to check with the module settings, etc...
In node.tpl.php try
<?php print $content ?>
However,
<?php print $node->content['body']['#value']; ?>
works for me as well.
To get control over your teaser length the master value is set with Post Settings.
(Length of trimmed posts)
To control this by node type try: http://drupal.org/project/teaserbytype
NOTE: Teasers are cached so you'll need to http://drupal.org/project/retease
However, if you want to just get it done in the node template you could run a node_load() and have everything... but that's not the best practice.
FYI: you can control what CCK fields show up in $content under Display Options.
PS: In teaser mode I often make use of truncate_utf8().

Getting two different fields from a thumbnail generated by DrupalĀ“s Image Cache and the original image

I have an image field, which imagecache automatically creates a thumbnail for.
I am creating my own node view, in which I would like to show both the full size image and the thumbnai, but drupal treats them as a single field (one or the other will be shown depending on which option you choose in "Display Views").
How can I make ImageCache treat the original image and the thumbnail, as two separate fields, or two different keys in the field array?
What module did you use for image attaching to node? imagefield, image, ...?
However, one way is theming node (if you use CCK and imagefield modules):
Create node-{YOURNODETYPENAME}.tpl.php (you can take source from node.tpl.php) in your theme folder.
And add there this code (image field named as image, also you should disable output of this field in settings of fields of this content type, or remove one of print... line):
<?php
$img = current($node->field_image);
$alt = $img['data']['description'] ? $img['data']['description'] : $title;
...
print theme('imagecache', 'YOURIMAGECACHENAME', $node->img['filepath'], $alt);
...
print theme('image', $node->img['filepath'], $alt);
...
?>

Resources