简体   繁体   中英

wordpress bloginfo('template_directory'); parsing as a string using twig

I'm trying to use a wordpress function which leads your path to the root of your theme in wordpress by doing:

<img src="<?php bloginfo('template_directory'); ?>/images/art.svg" alt="">

The issue is that if I inspect this I realise that the php code is parsed as a string instead of inserting a path to root of the theme. What am I doing wrong?

FYI: I'm using twig.

I've used Twig within Symfony, but never within WordPress. From my experience, in order to access functions such as bloginfo you will need to extend Twig to allow it.

This will require quite a bit of deep integration, and probably out of the scope of what you're asking for here.

More information: http://twig.sensiolabs.org/doc/advanced.html#functions

That said, it appears someone has done the hard work for you. Timber is a WordPress to Twig implementation: http://upstatement.com/timber/

Edit: More here , basically the syntax is {{function('FUNCTION NAME', 'ARGUMENT ONE', 'ARGUMENT TWO')}} , so in this case: <img src="{{function('bloginfo','template_directory')}}/images/art.svg" alt="">

In Timber the site object holds all your answers. For example...

<?php
$context = Timber::get_context();
Timber::render('single.twig', $context);

Then in your twig template...

<img src="{{site.theme.link}}/images/art.svg" /> 

... for the current theme directory (the equivalent of stylesheet_directory). Or:

<img src="{{site.theme.parent.link}}"/images/art.svg" />

... for the current parent theme directory (the equivalent of template_directory).

<img src="<?php echo bloginfo('template_directory'); ?>/images/art.svg" alt="">

你忘了回复这些信息。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM