0

I'm using a template that has this on the portfolio.tpl:

enter image description here

But I don't want to display the date of node created. So, I added in the content type portfolio a field "date_field" (please see picture)enter image description here

What I need now is to display this field on the tpl, replacing the "$node->created".

This is what I tried:

enter image description here

But it doesn't work.

How can I accomplish this?

2 Answers 2

0

Try

<?php print format_date($node->field_date["und"][0]["value"], 'custom','d M Y'); ?>
8
  • It returns this: Warning: date_timezone_set() expects parameter 1 to be DateTime, boolean given in format_date()
    – Drii7
    Commented Feb 26, 2016 at 11:07
  • Just do a print of the date to see what value you have: print render ( $node->field_date["und"][0]["value"] ) ; It seems to me that it is a matter of the format. I have tested the code above and works perfectly. But the value was in seconds, if you already have in date format, you are done with the line I copied before. As for print render $content["field_date"], it won't work.
    – Cesar
    Commented Feb 26, 2016 at 11:11
  • You don't need to wrap format_date() in a render(). And it is preferable to use [LANGUAGE_NONE] instead of ["und"].
    – dmsmidt
    Commented Feb 26, 2016 at 11:13
  • You can use dpm, Devel module or just do echo "<pre>";print_r($node);echo "</pre>" to see all variables available to .tpl.php. Don't be afraid if you see thousands and thousands of lines, for a single page Drupal loads tonns of data and metadata and loads memory as if it were a WBH ( Web-Black-Hole ).
    – Cesar
    Commented Feb 26, 2016 at 11:13
  • dmsmidt, it depends on the source of your data. If the value is already a date in legible (human readable format), you are right, otherwise you will use format_date. Here the problem is that $content['field_date'] is not available to *.tpl.php, but node->field_date[][][] is.
    – Cesar
    Commented Feb 26, 2016 at 11:16
0

Make sure that the field is visible under 'Manage Display' for the view mode you are working with.

If you setup the field to display in a format you like you can also just do this in your template file (instead of the format_date() part):

<?php print render($content['field_date'] ?>

In any case make sure that you also do the following before $content is rendered:

<?php print hide($content['field_date']) ?>
2
  • Thank you, but what I really want is to display in that specific format date.
    – Drii7
    Commented Feb 26, 2016 at 11:09
  • Sidenote: You can add a custom date format in Drupal, which you then can reuse across your site.
    – dmsmidt
    Commented Feb 26, 2016 at 11:14

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.