Hello I'm not really into PHP. I want to add multiple advanced custom fields to post/product archive in Wordpress. Here is the code I have that works but it only adds one field. Any help will be greatly appreciated.
add_filter('nectar_post_grid_excerpt','salient_mod_post_grid_excerpt');
function salient_mod_post_grid_excerpt($excerpt) {
global $post;
$custom_meta = '';
// Grab custom ACF field value.
if( function_exists('get_field') && isset($post->ID) ) {
$custom_meta = get_field( 'my_field', $post->ID );
}
$custom_excerpt = $excerpt . $custom_meta;
return $custom_excerpt;
}
To add multiple advanced custom fields to the post/product archive in WordPress, you will need to use the same get_field function to grab the values of each field, and then concatenate them to the excerpt in the same way as you are doing with the current field. Here's an example of how you can add two more fields to the code you provided:
add_filter('nectar_post_grid_excerpt','salient_mod_post_grid_excerpt');
function salient_mod_post_grid_excerpt($excerpt) {
global $post;
$custom_meta = '';
// Grab custom ACF field values.
if( function_exists('get_field') && isset($post->ID) ) {
$custom_meta = get_field( 'my_field', $post->ID );
$custom_meta_2 = get_field( 'my_field_2', $post->ID );
$custom_meta_3 = get_field( 'my_field_3', $post->ID );
}
$custom_excerpt = $excerpt . $custom_meta . $custom_meta_2 . $custom_meta_3;
return $custom_excerpt;
}
Make sure to replace the field names ('my_field_2', 'my_field_3') with the actual field names of the custom fields you want to add. Also, you can use a loop to add multiple fields dynamically.
$custom_meta = "";
if( function_exists('get_field') && isset($post->ID) ) {
for($i=1;$i<=3;$i++)
{
$custom_meta .= get_field( 'my_field_'.$i, $post->ID );
}
}