i have an image in my theme's "images" directory .. is there a function that will return the path to that image, so the image shows up in all views of my drupal site? I thought there was one, but reading the docs has not turned it up

Comments

dmitrig01’s picture

use an asolute path in you theme, such as /dir/to/drupal/themes/youthemename/images/youimagename
Hope this helps.

frjo’s picture

Use variable "base_path" and function drupal_get_path() like this.

PHPTemplate theme:

<?php print $base_path . $directory .'/image.png'; ?>

Plain PHP theme:

global $base_path;
$output .= $base_path . drupal_get_path('theme', 'your_theme_name') .'/image.png';
rapidsynergy’s picture

You could also use the function path_to_theme() - that way you don't have to hard code the theme name.

spencersundell’s picture

This is true, however note that per the API docs it will only give "the path to the currently selected theme." This can have undesired effects in some situations.

That is to say: if you are using an Administration Theme different from the site's default theme, and your code is being used in the admin section, using path_to_theme() will give you the path to the Administration Theme -- which would indeed be the "currently selected" one -- and not the default theme being used on the public side.

In other words, if you have a module or function that's trying to access the default theme within the context of an Administration Theme, path_to_theme() will give you no joy.

However, @rapidsynergy still raises a very valid point: what if the default theme changes? Fortunately, the code snippet from @frjo (which finally got me down off the ledge -- thanks!) can be enhanced to be portable.

The following works for me in Drupal 5.7:

  $output .= $base_path . drupal_get_path( 'theme', variable_get('theme_default', '0') ) .'/whatever.png';

The key is to replace "my_hardcoded_theme_name" with a call to variable_get that retrieves the name of the default theme. Voila...you've got joy even if the default theme gets changed. 'Course, you still have to make sure that theme has whatever file it is you're pointing to. :-)

Hope that's useful to someone else. Cheers...

ericpai’s picture

Thanks spencersundell ! It works for me in 6.20.