Support

Account

Home Forums Front-end Issues Title for referenced Post Object not right

Solved

Title for referenced Post Object not right

  • I’m actually not certain if this is an ACF issue, or just something generally wrong with my loop.

    I’ve added a repeater field to a WooCommerce Product. That repeater field sub_field is a Post Object.

    I’m having no trouble echoing out the permalink, image, and content from that object, but the title keeps echoing the id number of the referenced object, but the title of the Product instead of the title of the post object. Following is my code. I can’t see *why* I’m having this difficulty:

    <?php if( have_rows('cast') ): ?>
     
            <ul class="events">
     
                <?php while ( have_rows('cast') ) : the_row(); ?>   
     
                    <li>
     
                        <?php $post_object = get_sub_field('choose_cast_members'); ?>
     
                        <?php if( $post_object ): ?>
                        
                        <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post_object->ID), 'thumbnail' );
    		$url = $thumb['0']; ?>
     
                            <?php $post = $post_object; setup_postdata( $post_object ); ?>
     
                            <a href="<?php the_permalink($post_object->ID); ?>"><img src="<?php echo $url; ?>" alt="<?php the_title($post_object->ID); ?>" /></a>
                            
                            <h3><?php the_title($post_object->ID); ?></h3>
                            
                            <?php the_content($post_object->ID); ?>
     
                            <?php wp_reset_postdata(); ?>
     
                        <?php endif; ?>
     
                    </li>
     
                <?php endwhile; ?>
     
                </ul>
     
    <?php endif; ?>
  • You already got the post_object, you dont need to setup_postdata and in some functions you have to pass the id as integer, not a function (get_field)…

    So you can echo directly from the post_object, something like this:

    <?php if( have_rows('cast') ): ?>
            <ul class="events">
                <?php while ( have_rows('cast') ) : the_row(); ?>   
                    <li>
                        <?php $post_object = get_sub_field('choose_cast_members'); ?>
                        <?php if( $post_object ): ?>
                            <?php 
                              $thumbid = get_post_thumbnail_id($post_object->ID);
                              $thumb = wp_get_attachment_image_src( $thumbid ), 'thumbnail' );
                              $url = $thumb['0']; 
                            ?>
                                <?php //$post = $post_object; setup_postdata( $post_object ); //dont need to setup_post_data ?>
                                <a href="<?php echo get_permalink($post_object->ID);//here ?>"> 
                                    <img src="<?php echo $url; ?>" alt="<?php echo $post_object->post_title; //here ?>" />
                                </a>
                                <h3><?php echo $post_object->post_title; ?></h3>
                                <?php echo apply_filters('the_content', $post_object->post_content); ?>
                                <?php //wp_reset_postdata(); ?>
                        <?php endif; ?>
                    </li>
                <?php endwhile; ?>
                </ul>
    <?php endif; ?>
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.