Home › Forums › ACF PRO › How do I display a custom field in another custom post type as a shortcode? › Reply To: How do I display a custom field in another custom post type as a shortcode?
function related_post_acf_shortcode( $atts ) {
// extract attributs
extract( shortcode_atts( array(
'field' => '',
'related_field' => '',
'post_id' => false,
'format_value' => true
), $atts ) );
// get unformatted value of post object field
$related_post_id = get_field($field, $post_id, false);
// get value from the related post and return int
$value = get_field( $related_field, $related_post_id, $format_value);
// array
if( is_array($value) ) {
$value = @implode( ', ', $value );
}
// return
return $value;
}
add_shortcode('related_post_acf', 'related_post_acf_shortcode');
/*
shortcode
[related_post_acf field="post_object_field_name" related_field="field_name_on_other_post"]
*/
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.