Imagine a gardening blog say, and that when you create a post about strawberries (my favourite!) you tick strawberry in the fruits checkbox. Then when the post is published it automagically inserts some text on growing strawberries.
I can probably do this in PHP
[pseudo] if(value == strawberry) echo ( strawberry.txt );
with strawberry.txt held somewhere on the website.
But, it would be nice to get acf to hold the text in strawberry.txt so that when I add a new option to the fruits checkbox, I can also add the growing instructions at the same time. Can I do this?
(feel free to move this or to suggest a better title!)
I think it will be great if you add the growing text as a custom post type instead (you can use a plugin like Custom Post Type UI). By using a custom post type, you can add extra data you need in the text. After that, instead of using a checkbox you can use the post object field type.
Please keep in mind that you still need to modify your template to show the data.
I hope this makes sense.
Thanks James … but I might need this spelling out a bit.
Do you mean
– create a custom post type called growing-instruction, say
– create a strawberry growing-instruction post with the instructions in the content field
– in my blog post acf create a new post-object-field which looks for growing-instruction custom post types
– when I create a post relating to strawberries, I choose strawberry from the growing-instruction post-object-field
– behind the scenes, I add something to functions.php to grab and display the content from the growing-instruction post-object-field in the post
I’ve read the documentation and don’t quite understand how I grab info from a post that’s not _this_ post; could you help with this too, please?
Yes, you’re correct. You can also separate the instruction to field-based so it will have something like the instruction (using the standard content editor), growth time (text/number field), type of soil (text/select field), etc. That way, it will be more organized.
The post object field documentation has an example on how you add it in your template file. But you need to add it to the right files. If you want to show it on your post page, you need to add the code in the single.php file. This page should give you more idea about it: https://codex.wordpress.org/Templates.
I hope this helps 🙂
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2023 Advanced Custom Fields.