简体   繁体   中英

WordPress ACF Relation field loop out pages

I have created a Relation field to create a little menu with a few links in it. I've selected my pages and wrote the loop with the help of the documentation provided on the ACF website. The problem is the loop doesn't seem to be working. I get no errors and there's nothing to see, when I try to debug and dump the variable that is supposed to get the data it says NULL.

Anyone knows what is going wrong here? Been trying to fix it for multiple days now :/

Here's my loop:

<?php 

    $posts = get_field('field_56ebc552c03cb');

    if( $posts ): ?>
        <ul>
        <?php foreach( $posts as $p ): ?>
            <li>
                <a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a>
            </li>
        <?php endforeach; ?>
        </ul>
    <?php endif; ?>

I' ve found the issue, since I am using a field in my custom options page to retrieve the data I should use this:

$menu_posts = get_field('footer_links', 'option');

And my loop should look like this:

<?php 

    $menu_posts = get_field('footer_links', 'option');

    if( $menu_posts ): ?>
        <ul>
        <?php foreach( $menu_posts as $p ): ?>
            <li>
                <a href="<?php echo get_permalink( $p ); ?>"><?php echo get_the_title( $p ); ?></a>
            </li>
        <?php endforeach; ?>
        </ul>
    <?php endif; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM