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

对着背影说爱祢 提交于 2019-12-02 02:01:27

问题


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



来源:https://stackoverflow.com/questions/5353801/drupal-7-adding-an-image-to-a-node-tlp-php-theme-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!