Home › Forums › Feature Requests › Get custom field inside query loop block › Reply To: Get custom field inside query loop block
Just popping in here to say that a dedicated ACF field block DOES work! @uniondesign Maybe when you posted this they did not, but creating a dedicated custom block to output an ACF field inside a Query Loop block does work just fine.
Here is some example code:
function register_acf_block() {
// Check function exists.
if( function_exists('acf_register_block_type') ) {
// Register a custom block.
acf_register_block_type(array(
'name' => 'acf_contact_title',
'title' => __('Contact Title'),
'description' => __('A custom block to display the Contact title (ACF field) for Team Members inside the Query Loop block.'),
'render_callback' => 'render_acf_block',
'category' => 'formatting',
'icon' => 'admin-comments',
'keywords' => array( 'contact', 'title' ),
));
}
}
add_action('acf/init', 'register_acf_block');
function render_acf_block( $block ) {
// Get the post ID
$post_id = get_the_ID();
// Get the contact title ACF field
$contact_title = get_field('contact_title', $post_id);
// Display the contact title
echo '<p>' . $contact_title . '</p>';
}
It may be a little clunky but you can put all the “field blocks” into their own category so it doesn’t get confusing for Editor users.
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.