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?

Read More

FYI: I’m using twig.

Related posts

Leave a Reply

3 comments

  1. 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="">

  2. 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).