简体   繁体   中英

Advanced custom fields wordpress php

I am trying to use the relationship field but just cannot grasp the concept. It seems to be able to do exactly what I want but can't seem to understand it.

What I am trying to do is:

I have two custom post types: 'Shops' and 'Products'. I have created the two single- pages and the shops and products display exactly as I want it to. But what I want to do is create a button on the 'Shop' page that link to all products that is in the 'Shop' relationship field. And Visa Versa, link the 'Product' to the shop that sells it.

Please can anyone explain how I can achieve this as I am lost.

Thanks in advance.

From the ACF Docs , a loop to display all of the posts in the relationship with links:

<?php 

$posts = get_field('relationship_field_name');

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

I would recommend reading that whole article to get more familiar with relationship fields.

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