Last active
May 24, 2017 17:25
-
-
Save verticalgrain/1a5669d21ecaa8615c4703ca99b15ca3 to your computer and use it in GitHub Desktop.
Wordpress Timber cheat sheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Including twig files with vars | |
{% include "button.twig" with { text: "Click me" } %} | |
// Debug (enable WP debugging) | |
{{post|print_r}}// deprecated | |
{{dump(post)}} | |
// Image URL with custom image size | |
{{ TimberImage( fieldname ).src('large') }} | |
// Image with width and height | |
<img src="{{ TimberImage( fieldname ).src('large') }}" width="{{ TimberImage( fieldname ).width }}" height="{{ TimberImage( fieldname ).height }}" /> | |
// Taxonomy terms in a post | |
{{ post.terms('category') | join(', ') }} | |
// Taxonomy terms in a post, with loop | |
<ul class="list-terms"> | |
{% for term in post.terms('tags') %} | |
<li class="term-item"><a href="{{term.link}}">{{term.name}}</a></li> | |
{% endfor %} | |
</ul> | |
// Access Custom Field in PHP and Twig: | |
// in PHP: | |
$category->custom_field_name | |
$category->get_field('custom_field_name'); // get_field method | |
// in Twig: | |
{{ category.custom_field_name }} // Directly via property | |
{{ category.get_field('custom_field_name') }} // Via get_field method | |
// Access post author fields in Twig: | |
{{post.get_author.name}} | |
{{ TimberImage( post.get_author.author_photo ).src('thumbnail') }} | |
// Console Log something to browser console | |
<script>console.log('{{post|json_encode()}}');</script> | |
// Run wordpress function in Twig: | |
<p class="entry-meta">{{ function('get_the_title', post.ID) }}</p> | |
//Use a shortcode in a twig template | |
{% filter shortcodes %} | |
[gravityform id="1" title="true" description="true" ajax=true] | |
{% endfilter %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment