We'll create fresh WordPress site with NP Twig: Front End for Custom Field & ACF installed. You have 20 minutes to test the plugin after that site we'll be deleted.
tpl-{{post_type}}
Note: WordPress Classic Editor is preferred to edit templates
{# comments #}
for comments, and comments will not render to the user,Variables in templets are post title, post object, and custom fields. To output post title you can use {{ title }}
. If your post has a custom field called price. We use {{ price }}
to output the price. Use a dot (.) to access attributes of a variable (methods or properties of an object, or items of an array: {{ foo.bar }}
The if
statement in Twig check if a variable has a value or an expression evaluates to true
We can add conditions to display any text. Let’s say that we have a custom field named “agent” and want to display a text when the field agent contains a text. We can write
{% if agent %} We have an agent in your area Our agent: {{ agent }} {% endif %}
another example; let’s say that we have a field named price. We want to display a text if the price is zero
{% if price == 0 %} <p>This product is free</p> {% endif %}
Please note that we use the operator ==
to check the equality You can also test if an array is not empty:
{% if meanings %} <p>The array or repeater field **meaning** contains some values</p> {% endif %}
Always use {% endif %}
to close the previous if condition, Read more about if keyword in twig
If our field or variable is an array or a repeater field we can loop over each item in a sequence using for keyword
The for statement code example
{% for m in meanings %} Meaning: {{ m.meaning }} {% endfor %}
The for statement format
{% for m in meanings %}
to loop over an array or repeater field called meanings
using m
to mention for each row**m.**
{% endfor %}
for
keyword in twig{% <del><span></del> endfor <del></span></del> %}
Using if, endif, for and endfor
allows to generate a powerful template; however, twig contains many other useful keywords to see other keyword and how to use them visit twig documentation
<h2>Meaning of {{ title }}</h2> {# This is a comment and will not render #} {% for m in meanings %} {% if m.meaning %} Meaning: {{ row.meaning }} {% endif %} {% if m.example %} Example: {{ m.example }} {% endif %} {% endfor %}
We can access the post using the post keyword. to access any property of the post we put a dot (.) and then we put the property name for example use post.content
to access post content
{{ post.title }} {{ post.content }}
read more about accessing post content on timber documents
This plugin is distributed in the hope that it will be useful, but without any warranty. See the GNU General Public License for more details. http://www.gnu.org/licenses/.
get_fields_{{post_type}}