Support

Account

Home Forums Add-ons Repeater Field Get post ID from Post Object in Repeater Field

Solved

Get post ID from Post Object in Repeater Field

  • Hi, I have a repeater field that includes a post object field, but I am unable to pull the post ID for the custom type post being selected.

    In practice, I have a logo carousel that I want each logo to link to the anchor section in the Work page containing all the work sections for each corresponding logo. Each work section has an anchor based on the post type ID (id=”work-[$post->ID]”).

    I’ve based my code on the post object field example (see below), but it causes the page to terminate early. To keep it short, I’ve removed the image upload field portion of the code.

    Any insights would be greatly appreciated.

    <ul id="list-our-clients">
        <?php while(the_repeater_field('home_clients_logos')): ?>
    	<?php $clientLink = get_sub_field('client_link'); ?>
     	<li><a href="<?php echo get_permalink(28); ?>
                <?php if( $clientLink ) : ?>
                    <?php foreach( $clientLink as $post): ?>
                        <?php setup_postdata( $post ); ?>#<?php the_sub_field('client_link', $clientLink->ID); ?>
                        <?php wp_reset_postdata(); ?>
                    <?php endforeach; ?>
                <?php endif; ?>"></a>
            </li> 		
        <?php endwhile; ?>
    </ul>
    • Elliot

    • December 10, 2013 at 4:39 pm

    Hi @tag_webdev

    I believe the issue is that your code is using $clientLink (an array) as a post object. The issue is with this line:

    
    the_sub_field('client_link', $clientLink->ID);
    

    I think it should be:

    
    the_sub_field('client_link', $post->ID);
    

    Just to make sure, the field named client_link is a sub field within a repeater and returns multiple posts? Does this post_object field allow multiple selections or just one?

    Thanks
    E

  • I just made the change to the code. It’s the same result, unfortunately.

    Is the fact that it is inside of a query_posts while loop a potential issue? Would I need to reset the post data just before this code starts?

  • Hi Eliot,

    I figured it out — with your guidance of course!

    I updated the code based on your sample that takes just one selection to the following:

    <?php while(the_repeater_field('home_clients_logos')): ?>
        <?php $clientLink = get_sub_field('client_link'); ?>
         <li><a href="<?php echo get_permalink(28); ?>
            <?php if( $clientLink ) : ?>
                <?php $post = $clientLink; ?>
                <?php setup_postdata( $post ); ?>#<?php echo get_the_ID(); ?>
                <?php wp_reset_postdata(); ?>
            <?php endif; ?>"></a></li> 		
    <?php endwhile; ?>

    I replaced:
    <?php the_sub_field( 'client_link', $post->ID ); ?>

    with:
    <?php echo get_the_ID(); ?>

    Thanks again for your help and super-quick response!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.