Drupal 7 - Adding an image to a node.tlp.php theme

2019-02-27 12:02发布

问题:

I am trying to add an image (small arrow gif icon) which is uploaded to the image directory within my Drupal theme files (root/sites/all/themes/mytheme/images).

The following works at the page.tlp.php level, and it also is working at the field.tlp.php level - but it won't work in node.tlp.php. The node.tlp.php file is working effectively but the image doesn't show! If I paste exactly the same code into the other afore mentioned templates it does show..??

<img src="<?php print base_path() . path_to_theme(); ?>/images/arrow-right.gif" width="20" height="13" alt="Arrow Right">

Any ideas how I should reference an image in a node.tlp.php file?

Thanks!

回答1:

Using drupal_get_path() instead of path_to_theme() may work, worth a shot

<img src="<?php print base_path() . drupal_get_path('theme', 'THEMENAME'); ?>/images/arrow-right.gif" width="20" height="13" alt="Arrow Right">

This was suggested Here



回答2:

I'm wondering why not us the $directory variable which is available in node.tpl.php file. It was available in Drupal 6 and seems to be working for me for D7 as well.

So code qould be something like:

<img src="/<?php print $directory; ?>/images/arrow-right.gif" width="20" height="13" alt="Arrow Right">


回答3:

Use the GLOBAL variables array. They are always available because they are, well, global. They are available as an array.

I would just create a new variable in template.php to keep your template files clean:

   // helper variable path to theme
function mytheme_preprocess_node(&$vars) {
$vars['thefullpath'] = $GLOBALS['base_url'] . "/" . $GLOBALS['theme_path'];
}

Then in your template file:

<img src="/<?php print $thefullpath; ?>/images/arrow-right.gif" width="20" height="13" alt="Arrow Right">

Documentation here: http://api.drupal.org/api/drupal/globals/7