So I am having a twig/timber
issue where I have a difficult time getting a placeimage set, here is what I have so far:
{% set defaultimg = site.theme.link ~ '/images/placeimage.png' %}
and then later in my code I have:
<img src="{{ post.thumbnail.src | default(defaultimg) | resize(360, 240)}}" class="card-img-top">
post.thumbnail.src pulls in the images just fine but if no featured Img in attached to the post I want the defaultimg
to pull in an image from my custom themes image folder. But currently this is outputting a really erroneous url of:
<img src="https://toolcart.local/wp-contentC:\Users\User\Local Sites\toolcart\app\public\wp-content\themes\toolcart-theme\images/placeimage-360x240-c-default.png" class="card-img-top">
However a {{defaultimg}}
outputs the image url correctly.
https://toolcart.local/wp-content/themes/toolcart-theme/images/placeimage.png
I am not sure what to try next?
Custom Twig File # If you're using the Timber Starter Themeyou can Create a file called page-about-us.twiginside your viewsand go crazy. Copy and paste the contents of page.twigso you have something to work from. How does this work?
There are a few ways to manage custom pages in WordPress and Timber, in order from simple to complex. Custom Twig File # If you're using the Timber Starter Themeyou can Create a file called page-about-us.twiginside your viewsand go crazy. Copy and paste the contents of page.twigso you have something to work from.
Blocks are the single most important and powerful concept in managing your templates. The official Twig Documentation has more details. Let’s cover the basics. In single.twig you see opening and closing block declarations that surround the main page contents. {# other stuff here ... #}
Things like the site name, the description or the navigation menu you’ll want to start with each time (even if you over-write them later). You can do a print_r ( $context ); to see what’s inside or open-up Timber.php to inspect for yourself.
This is an issue with the way twig/timber
defines paths!!! Supper weird way of doing things which adds unnecessary complexity to a project.
That being said, according to their documentation, you could add a filter called relative
to the end of your defaultimg
variable which converts an absolute URL into a relative one which eventually outputs the correct path to your default image.
So your code would be something like this:
{% set defaultimg = Image(site.theme.link ~ '/images/placeimage.png') | relative %}
NOTE:
The | relative
filter will do the trick here.
<img src="{{ post.thumbnail.src | default(defaultimg) | resize(360, 240)}}" class="card-img-top">
I just tested the code and it worked fine! Le me know if you could get it to work too!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With