Home › Forums › General Issues › Post object & Fields on Blog page
Hi all, I’m trying to show a post object with some custom fields on my blog page. My home page is set as a static page, and my posts page (index.php) is looking at a page called “blog”. The ID of the posts page is 674.
Here’s the code I’m using:
<?php $post_object = get_field('prospectus', 674);
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<div class="side-box side-prospectus">
<img src="<?php the_field('image', 674); ?>" alt="<?php the_title(); ?>">
<h3><?php the_title(); ?></h3>
<p><?php the_field('description', 674); ?></p>
<?php
$file = get_field('pdf_file', 674);
if( $file ): ?>
<a href="<?php echo $file['url']; ?>" target="blank" class="button small button-icon-right button-icon-download"><?php _e('Download (PDF)','anglocontinental') ?></a>
<?php endif; ?>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Every instance of <?php the_title(); ?>
is working correctly, but none of the custom fields are displaying.
I’ve searched around and tried several things to try and fix it, one attempt was to add the posts page ID to each field call (shown above), another was to add get_option('page_for_posts')
to each field call, but still without success.
Any and all assistance greatly appreciated.
Ah, I’ve sorted it!
I only needed to add the posts page ID to the first call, like this:
<?php $post_object = get_field('prospectus', 674);
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<div class="side-box side-prospectus">
<img src="<?php the_field('image'); ?>" alt="<?php the_title(); ?>">
<h3><?php the_title(); ?></h3>
<p><?php the_field('description'); ?></p>
<?php
$file = get_field('pdf_file');
if( $file ): ?>
<a href="<?php echo $file['url']; ?>" target="blank" class="button small button-icon-right button-icon-download"><?php _e('Download (PDF)','anglocontinental') ?></a>
<?php endif; ?>
</div>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Where (in which php-file) did you placed that code? Same problem here!
Thank you in advance!
The topic ‘Post object & Fields on Blog page’ is closed to new replies.
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.