我如何获得当前drupal主题的路径?

Ste*_*ble 24 drupal drupal-6 drupal-theming

Drupal API具有drupal_get_path($type, $name)任何特定主题或模块的路径.如果我想要当前主题的路径怎么办?

FGM*_*FGM 25

使用该path_to_theme功能.

  • 请注意,当从"theme_*"函数内部调用时,path_to_there将返回当前模块的路径,而不是可能导致错误的当前活动主题的路径,请参阅此错误报告中的更多详细信息:http://drupal.org/node/194098 (3认同)

Owe*_*wen 20

这应该工作(doc):

global $theme;
$path = drupal_get_path('theme', $theme);

// there's also a $theme_path global

global $theme_path;
Run Code Online (Sandbox Code Playgroud)

  • 为什么使用 path_to_theme() 而不是 $theme_path 更好? (2认同)

小智 10

在D6中,path_to_theme()可能不会以您期望的方式运行,具体取决于您使用它的方式.如果你在任何主题预处理函数之外使用它,那么它可能会给你你想要的东西,但是如果在模块的主题/预处理钩子函数的上下文中调用它...它将指向模块路径宣布主题.

防爆.如果我有一个主题"my_theme"和我的模块"my_module"使用预处理挂钩覆盖论坛主题,则在我的模块中调用path_to_theme():例如my_module_preprocess_forums()...将返回"论坛",而不是"my_theme" "正如人们所预料的那样.

如果你问我,果味很浓.


小智 10

在 Drupal 8 中,如果您需要在管理主题处于活动状态时获取活动主题路径,您可以获取默认主题路径:

$themeHandler = \Drupal::service('theme_handler');
$themePath = $themeHandler->getTheme($themeHandler->getDefault())->getPath();
Run Code Online (Sandbox Code Playgroud)

  • 我认为 Drupal 8/9 的最佳答案 (3认同)

Pup*_*pil 5

在Drupal 7中,为了获取当前主题的路径,我们可以使用: path_to_theme()函数。


小智 5

在 Drupal 8 中

global $base_url;
$theme = \Drupal::theme()->getActiveTheme();
$image_url = $base_url.'/'. $theme->getPath() .'/images/image.jpg';
Run Code Online (Sandbox Code Playgroud)