Latest blog posts

header ads

how to show all selected acf field with label using php and make shortcode

Create a Custom Shortcode Function: In your theme's functions.php file or in a custom plugin file, create a function that will be used as the shortcode handler. This function will retrieve and display the selected ACF fields with their labels.


function display_acf_fields_shortcode($atts) { // Shortcode attributes (if any) can be used here // Retrieve ACF fields using get_field function $field1 = get_field('your_acf_field_1'); $field2 = get_field('your_acf_field_2'); // Add more fields as needed // Display the fields with labels $output = '
    '; $output .= '
  • Field 1 Label: ' . esc_html($field1) . '
  • '; $output .= '
  • Field 2 Label: ' . esc_html($field2) . '
  • '; // Add more field labels and values as needed $output .= '
'; return $output; } // Register the shortcode add_shortcode('acf_fields', 'display_acf_fields_shortcode');


Replace 'your_acf_field_1', 'your_acf_field_2', etc., with the actual names or keys of your ACF fields. Usage in WordPress Editor: You can now use the [acf_fields] shortcode in the WordPress editor to display the selected ACF fields with labels.


Displaying Shortcode in Theme Files: If you want to use the shortcode directly in your theme files (e.g., single.php or page.php), you can use the do_shortcode function:

Post a Comment

0 Comments