kayue / KayueWordpressBundle

A Symfony 2 bundle for providing WordPress repositories and authenticating users (login).
101 stars 43 forks source link

Post Thumbnail #81

Closed pmpr closed 6 years ago

pmpr commented 6 years ago

How should one get post thumbnail in twig?

    {% set img = wp_find_featured_image(post) %}
    {{ img.slug }}

does not give me the image url.

jgarib commented 6 years ago

You either need to grab the information yourself from the database, or you could set up a twig extension and have that extension call the wordpress function, provided you are loading wordpress as well as symfony in your project.

https://symfony.com/doc/master/templating/twig_extension.html

pmpr commented 6 years ago

Thanks for your response, but what do you mean by

provided you are loading wordpress as well as symfony in your project ??

I setup the KayueWordpressBundle, and now want to get post thumbnail. I am familiar with Twig extensions. But how should I call Wordpress functions from within Twig extension? AFAIK, the Wordpress core is not part of KayueWordpressBundle anymore:

Improved version of the original WordpressBundle. The biggest different is this new KayueWordpressBundle won't load the entire WordPress core, thus all the WordPress template funtions won't be available in your Symfony app. This is also the goal of the bundle; do everything in Symfony's way.

jgarib commented 6 years ago

To load the wordpress core, you need to include wp-load.php. How and where you do this depends on your project. There are some performance implications to loading both symfony and wordpress so it's best not to do it on every page.

https://wordpress.stackexchange.com/questions/47049/what-is-the-correct-way-to-use-wordpress-functions-outside-wordpress-files

Once you've included that file you should have access to wordpress functions and globals.

pmpr commented 6 years ago

Finally I found the solution. Required Twig functions are provided by the bundle itself, out of the box:

    {% set img = wp_find_featured_image(post) %}
    {% if img is defined and img is not null %}
        {% set url = wp_get_attachment_url(img, 'medium') %}
        <img src="{{ url }}">
    {% endif %}