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 don’t seem to understand what @saltnpixels is trying to achieve. The result of the render_callback specified when registering a block through ACF is stored to database on save of the post.
The desired behaviour is to have the content generated on request. Thus the example of the latest three posts, which might be different each time a user views the post.
The solution to the problem is using the
render_block filter: https://developer.wordpress.org/reference/hooks/render_block
The second argument to this hook,
$block, is an array. You can compare
$block['blockName'] against your block name (beware of the
acf/ prefix) and return dynamic content when it matches.
$block['attrs']['data'] is an array of all custom fields you defined for the block.
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!
For anyone keeping up with my @wp_acf adventure today I actually made some progress with "ACF Shortcake" that mashes together the functionality of Shortcake and ACF Field groups inside ACF WYSIWYG fields.— Jon Christopher (@jchristopher) October 9, 2020
Any hot tips on enhancing Shortcodes to show previews a la [audio] et al? pic.twitter.com/g1mcOyMWIe
© 2020 Advanced Custom Fields. Subscribe