Can a dynamic block be created with ACF?
Meaning a block that just returns the latest three posts of a post type or something like that?
Or are the blocks made with ACF only capable of static content that gets saved?
There isn’t a reason to use ACF for something like this. The most recent posts of a post type can be done without ACF in WP using a query, there are many shortocde plugins that will do this. I’m not sure, but I’d guess that someones already built a block to do something similar with a CPT.
Why would there be no need?
I’m making a site. The user can choose to add a block that shows the latest portfolio items on their site. They can choose how many to show and I can give other options.
You could use acf field for blocks, but I can’t give you any specifics other than the acf block documentation here https://www.advancedcustomfields.com/resources/blocks/ and here https://www.advancedcustomfields.com/blog/acf-5-8-introducing-acf-blocks-for-gutenberg/
I just want to know if the fields made with acf can be dynamic like the Gutenberg native latest posts block.
Of course you can… why would you think you would not? The rendering file, is actually a php file, so what do you think would stop you. From the plugin repository if you download the “ACF Blocks” – https://wordpress.org/plugins/acf-blocks/ there is a Post Block just to prove that it is possible.
The reason to use Gutenberg blocks as oppose to shortcodes are very obvious, besides, your customers will prefer to click things to work, as oppose to using shortcodes.
I figured the blocks were all saved statically to the post content.
According to the handbook for Gutenberg extra work has to be done to make a dynamic block.
Let me try again !
1. ACF Blocks, unlike normal blocks, are 100% dynamic. https://www.advancedcustomfields.com/resources/blocks/ – The faq “Can I make changes to the template? confirms that fact.
2. Here is a video proof that it CAN be done with ACF – and I’m not done yet, I will be adding more features to it…
You can manipulate $post in your block and then use the standard post loop:
$posts = get_posts(array('post_type' => 'YOUR_POSTYPE', 'posts_per_page' => 3 ));
if( $posts ): ?> <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?> <?php setup_postdata($post); ?> // Your code <?php endforeach; ?> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?> <?php endif; ?>
If you want to give the user the option to change, let’s say the ammount of posts, then you can set the value of a field as variable in your array:
$number_of_posts = get_field('your field'); $posts = get_posts(array('post_type' => 'YOUR_POSTYPE', 'posts_per_page' => $number_of_posts ));
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!
It’s official! These brilliant minds (@tessak22 and @zgordon) are creating an entire course on ACF Blocks! Interested in Gutenberg but enjoy the comfortable recliner that is PHP? This course is for you! Pre-sale on now! 🔥🔥🔥 https://t.co/ifMzctgsN8— Advanced Custom Fields (@wp_acf) September 4, 2019
© 2019 Advanced Custom Fields. Subscribe