Home Forums General Issues foreach loop page_link so displays multiple link items


foreach loop page_link so displays multiple link items

  • Hi


    I have managed to load and display a selected page link value:

    <a href="<?php the_field('page_link'); ?>">Read this!</a>

    However, i am unable to display multiple items:

    // vars
    $urls = get_field('urls');
    // check 
    if( $urls ): ?>
    <h3>Further reading</h3>
    	<?php foreach( $urls as $url ): ?>
    		<a href="<?php echo $url ?>"><?php echo $url; ?></a>
    	<?php endforeach; ?>
    <?php endif; ?>

    My page is here, you can see the View 4ply link above the main image, this is the selected link.

    Any idea why the loop code isn’t working?


  • What kind of field is it? Is this a post object or a relationship field? Something else?

  • Hi John

    It’s a Relational page_link.

    I set these up, or am hoping to, so can add links to other products.

    I have noticed, though, that i am unable to set up a single link, not just multiple. I did think it was working, but just noticed it isn’t and just linking to itself, e.g. look at the Read this link under add to cart button here

    I am adding the code to this theme file content-single-product.php

  • A relationship field returns either an array of post IDs or an array of post objects rather than URLs. See the documentation for how to display values for these fields

  • I have added the first code snippet from this page

    $posts = get_field('relationship_field_name');
    if( $posts ): ?>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <span>Custom field from $post: <?php the_field('author'); ?></span>
        <?php endforeach; ?>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>

    I have added the code to content-single-product.php

    However, no links are displaying.

    Do you think it could be the $posts bit? i ask as the content type is product, it’s not a straight forward post content type.

  • * I did change relationship_field_name to page_link

  • changing posts to products and post to product did not work

  • If you’re using a relationship field I don’t see anything wrong with your code.

    If you changed the field type, did you also go an change all of the values already saved to the existing field? If not the data in those fields will not be saved correctly. That’s the only thing I can think of that might cause it to not work.

  • The fields have always been Relational, it’s just the code that has changed, to suit Relational, as per your suggestion in above post, which i thank you for.

    I did re set the fields and saved the post/page just to make sure but it made no difference.

    thanks John 🙂

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

The topic ‘foreach loop page_link so displays multiple link items’ is closed to new replies.