I am trying to put some Drupal taxonomy links into a twig template, but have hit a wall, hoping someone can help me out.
Here is the setup, and what I have tried so far...
I have a taxonomy vocabulary called "FAQ Category". Inside of it, there are several terms, for example, one of them is named "The Basics".
I also have a Drupal twig template named views-view--faqs.html.twig, which is displaying all the FAQ content types that have been tagged for a particular term. (...as a side question, I do not see a Drupal view that has been setup for this, so I dont know how views-view--faqs.html.twig is even working in the first place, but it is...)
What I need, is to have a simple navigation to each of the taxonomy terms on that template, but I dont know how to do it. I have tried things like:
{{ node.field_basics.value }}
{{ node.field_faq-category.entity.label }}
{{ content.faq_category.0.value }}
...but nothing is working. I am guessing there is a very simple solution, if anyone can help it would be much appreciated. This site was not developed by me, so I am doing a bit a reverse engineering.
If there is any more info you need to help get an answer, just let me know, I will provide as much info as I can.
Thanks in advance.
I would suggest you to take a look at the following guide:
https://www.drupal.org/docs/theming-drupal/twig-in-drupal/discovering-and-inspecting-variables-in-twig-templates
Here you can learn how to output values or even reading field keys.
I believe you should try either:
{{ node.field_basics.0.uri }}
or
{{ content.faq_category.0.uri }}
Related
Using timber/twig for wordpress. Trying to load JS but only for a certain page.
I figured this would be something like
{% if page = mypage %}
<link......./>
{% endif %]
Actually the answer to your question is not depending whether you use Timber or not. You should use conditional statements with the combination of WordPress enqueue functions.
You can see the examples of such approach for example here:
http://geoffgraham.me/wordpress-load-files-on-specific-pages/
The topic is broad and you have a variety of solutions you may use here. Nevertheless I hope my answer helps you move into the right direction.
I've recently taken over dev of a Wordpress site using Timber (which I wasn't familiar with). I'm attempting to use a new plugin and accompanying shortcode, which of course doesn't work.
I've been researching this for a couple hours and there doesn't seem to be a simple answer. In order to use a simple shortcode like this:
[sp_faq category="7"]
Do I really need to create a custom shortcode function in functions.php, add some sort of template file for it, etc? This seems counterintuitive to Twig's making things 'simple'.
The documentation for this is less than stellar unless I'm missing something obvious.
You could try this (example using a gravity form shortcode):
{% filter shortcodes %}
[gravityforms id="1"]
{% endfilter %}
If you want to render a shortcode from a custom field you can do it like this:
{{post.custom_shortcode_field|shortcodes}}
Taken from the docs:
https://timber.github.io/docs/guides/filters/#shortcodes
Or if it's comming from the main-editor try:
{{ post.content|wpautop }}
Seems like this is too late for OP, but for anyone else coming across this: The correct solution is combined in the Luckyfella's answer and the comments on it. I think OP would have got it working if he had tried Luckyfella's final suggestion.
You need to put {{post.post_content|wpautop|shortcodes}}* into your Twig file(s). This will render both auto paragraphs and shortcodes that are put into the main WYSIWYG editor, by default.
*post is simply the conventional default name in Timber for a TimberPost, you will also need to check in your PHP template files to see what the Timber context and Timber post variables are called. For OP it seems to have been page, not post.
Just thought I'd chime in here.
post.post_content contains the raw data that is contained in the database (before any filters have been applied to it) and post.content contains the data after the filters have been applied, so using the filter |shortcodes shouldn't be needed to be run on that.
I was using post.post_content for some reason and found this question because I was trying to figure out why my shortcodes weren't working and hence it led me to do some more research.
Now, reading the answers in this question, I wasn't really satisfied with as I had also been using |e('wp_kses_post') to sanatise the data that I output, but if I used something like this:
{{ post.content|wpautop|e('wp_kses_post') }}
...then obviously I would get the correct filtered data with the shortcodes processed, but it would also at the same time strip out any non-allowed data with the e('wp_kses_post') filter.
Sure, you could add allowable tags within this filter, but obviously that isn't very realistic as you don't know exactly what output the shortcodes will be outputting nor do you want to keep updating it.
So, we had a problem... we want to allow shortcodes to be parsed, but also sanatise the content at the same time - what to do!?
The solution is the below:
{{ post.post_content|wpautop|e('wp_kses_post')|apply_filters('the_content') }}
Here we use post.post_content so we have the content before it has been filtered, then after the wpautop filter it is followed by e('wp_kses_post'); this will sanatise the data, but the important part being it will leave shortcodes alone, so they can be still be filtered!
Lastly, we apply the filters for the content with the apply_filters filter, and this then takes care of all the filters applied to the_content including parsing the shortcodes.
Though if you are already using universal escaping you will have to consider how the above will apply to your situation.
Reads like you want to use a shortcode outside of post.content.
You can use {% function('do_shortcode', '[shortcode here]') %} to process a shortcode where ever you want to in a template.
I am having a look at the doc of KnpMenuBundle for SYMFONY,
In the part:
KNPMENU others rendering options; It lists a bunch of options that can be used when rendering the menu thru a TWIG file like this:
{{ knp_menu_render('AppBundle:Builder:mainMenu', {'depth': 2, 'currentAsLink': false}) }}
{{ knp_menu_render(menuItem) }}
I am wondering what those options do, I am especially wondering about the "currentAsLink" one. I can't find any link where those options have been detailed. Does anyone have more explanation about those functionalities?
I've created custom theme look for one of my fields in views, and added field field_field_services_text_link[0]['rendered']['#markup'];?>. I tried to do same with my another image field, but it doesnt works. I used field_field_services_image[0]['rendered']]['uri']; ?>, but something wrong in this code. Any ideas how to solve it?
To troubleshoot this, perhaps the following would help. Paste it in your theme file instead of what you have above
<?php print_r($field_field_services_image); ?>
the print_r function will show you the structure of the array ($field_field_services_image) you're trying to work with and can show you whether the structure is exactly as you'd expect. Perhaps there's a problem somewhere in the [0]['rendered']... portion, and you can check that. It may be empty somewhere, etc...
You could always see whats being output with a var dump or even better, the devel module.
I'm struggling to use custom post template in thesis. I would like to use different layouts for different posts. There are many tutorials for using custom page template but I can't find ones for posts. I'm new to Thesis so I probably miss something but if someone knows how to or tutorial that mentions about it, please share with me. Thanks in advance!
One way to do this (that goes against the thesis custom_functions.php file, unfortunately) is to use this plugin:
http://wordpress.org/extend/plugins/custom-post-template/
Then, create a page called, say, news.php and put this code inside
<?php
/*
Template Name Posts: News
*/
/*
Your custom code goes here
*/
Then, once you upload this to your thesis folder, it should show up in the post-template dropdown.
?>
Answer #1 in this post might provide you with a way to style posts in Thesis or elsewhere, as long as you're using WP 2.8+:
http://www.smashingmagazine.com/2009/10/20/10-useful-wordpress-hacks-for-advanced-themes/