Please help me, I am using advanced custom fields and I created a field of type object post where I selected the posttype "product", I want to display the image of the product inside the loop but I am not able to
I will leave below the code of the display loop
<?php
$featured_posts = get_field('featured_posts');
if( $featured_posts ): ?>
<ul>
<?php foreach( $featured_posts as $featured_post ):
$permalink = get_permalink( $featured_post->ID );
$title = get_the_title( $featured_post->ID );
$custom_field = get_field( 'field_name', $featured_post->ID );
?>
<li>
<a href="<?php echo esc_url( $permalink ); ?>"><?php echo esc_html( $title ); ?></a>
<span>A custom field from this post: <?php echo esc_html( $custom_field ); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
CodePudding user response:
It depends on how you setup your ACF POST object RETURN parameter. It can be set to ID or POST object. But it can't be set to post_title. In your given code you try to use title by that way.
Anyway, you can call it via native WP function. Like this:
<ul>
<?php foreach( $featured_posts as $featured_post ):
$permalink = get_permalink( $featured_post->ID );
$title = get_the_title( $featured_post->ID );
$custom_field_ID = get_post_meta( $featured_post->ID,'field_name',true);
if ($custom_field_ID){$custom_field_post_object=get_post($custom_field_ID);}
$custom_field_title='';
if(!empty($custom_field_post_object)){
$custom_field_title=$custom_field_post_object->post_title;
}
?>
<li>
<a href="<?php echo esc_url( $permalink ); ?>"><?php echo esc_html( $title ); ?></a>
<span>A custom field from this post: <?php echo esc_html( $custom_field_title ); ?></span>
</li>
<?php endforeach; ?>
</ul>
CodePudding user response:
the code is returning the title to me, I would just like it to return other post values as an image.