I have used Advancedcustomfields
plugin to create Advanced Custom Fields.
Now, want to get an Advanced Custom field value using post id.
I have tried below and get solution.
Now,please let me know if any other way I will get same result?
<?php
$post->ID='15';
$var_name= get_field('field_name', $post->ID);
/* field name means which u gave custom field name*/
echo $var_name;
?>
The solution you used seems correct. But if you still want to know alternatives, here it is:
$varname = get_post_meta($post_id, 'fieldname', true);
echo $varname;
$varname
will be an array if the last parameter is false
and will be the value of metadata field if the last parameter is true
.
You can use the_field()
as well which works similar to get_field()
you used.