John Davidson

php - Acf Post Object

0 comments
Message:


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; ?>


My Answer:
to display. Now I want to display the title of the selected post on my page. How can I achieve this in PHP?

You can achieve this by using the get_field() function provided by Advanced Custom Fields. Here's an example code snippet to display the title of the selected post:

php
// Get the post object field value
$post_object = get_field('your_post_object_field_name');

if( $post_object ):
$post = $post_object;
setup_postdata( $post );
?>


wp_reset_postdata();
endif;
?>


Replace 'your_post_object_field_name' with the actual name of your post object field. This code snippet will retrieve the selected post object, set it up as the current post, and then display the title of the post. Make sure to include this code within the loop on your page where you want to display the post title.

Rate this post

5 of 5 based on 6692 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us