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.
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/
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.
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!
© 2023 Advanced Custom Fields.