Attached is an image showing the editor. This content template is for a listings page which loops through a CPT and lists some information for each post. I've used a ACF (Advanced Custom Fields) field on this CPT, and I'd like to display the results here in my template. Is this possible, and if PHP is required, can it simply be added by opening the tag (as it would in a .php template file)? Or do I need to do something else?
The field I've added to the CPTs showing in my loop template here is "complementary_programs" and it's a relationship field - it returns an array of related post objects. I'd like to simply get the custom field (which contains an array of post objects) then loop through the posts objects and get the title and permalink, and add that to the output. In other words, lets say 5 related posts are selected for my custom field "complimentary_programs" titled Post 1, Post 2, etc. I want to display "Complimentary Programs: Post 1, Post 2, Post 3, Post 4, Post 5...". and link to each post.
I have absolutely no problem doing this in PHP and have done it many times using ACF on other sites, but I can't understand the content template editor options and how to use PHP here, or if it's possible. I assume that the "loop start" comment used at the start of the template means that we are within the loop here and should be able to do things like "get_field('my_custom_field') or "get_the_id()" to get the current CPT's ID. Is this correct? Do you have docs on using PHP in the content template and/or achieving my goal here?
Thanks,
Austin
Hi Austin,
Thank you for contacting us and I'd be happy to assist.
> I assume that the "loop start" comment used at the start of the template means that we are within the loop here and should be able to do things like "get_field('my_custom_field') or "get_the_id()" to get the current CPT's ID. Is this correct?
- Your understanding is correct and when you're inside the loop tags in a view ( i.e. <wpv-loop>...</wpv-loop> - https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-loop ), you'll be able to use Toolset and WordPress functions to get the information of the current CPT item in the loop.
> Do you have docs on using PHP in the content template and/or achieving my goal here?
- The PHP code can't be directly inserted into the content template and view's editor, but you can instead register a custom shortcode ( ref: https://codex.wordpress.org/Shortcode_API ) and return the desired output from the PHP code through it.
We also have a guide on this topic at:
https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar