Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have two post types:

  1. post type = stores
    which has a title and custom field (by ACF) storeurl.

  2. I have another post type called books with just title and custom field bookcode (by ACF).

I assigned the post type stores as "post object" to books....the code below get the concerned fields but displays the last post only... how do i get it to loop and display all the posts ?

<div class="bookdata">
        <?php $storelist = get_field('storelist');
          $bookcode = get_field('bookcode');    
            //var_dump($storelist);
         foreach ($storelist as $post) :  setup_postdata($post); 
        ?>
      <div class="storename">
         <?php echo get_the_title($post->id);?>
      </div>

      <div class="clickbtn">
                <a href="<?php echo get_field('storeurl',$post->id); ?>">
                    <?php echo $bookcode;?>
                </a>
      </div>
      <?php endforeach; wp_reset_postdata(); ?>    
</div> 

I tried the code below, it does display the posts but the bookinfo and linkbtn get repeated more than once within the bookdetails.

<article class= "bookgallery">
<?php 
while (have_posts()): the_post();?> 
<div class="bookindex">

     <div class="bookdetails">
        <?php       
        $bookcode = get_field('bookcode',$post->id);
        $bookinfo = get_field('bookinfo',$post->id);

        $storelist = get_field('storelist');            

         foreach ($storelist as $post) :  setup_postdata($post);    

        ?>
        <div class="storename">
            <?php echo get_the_title($post->id);?>
        </div>
        <div class="bookinfo">
            <?php echo $bookinfo; ?>
        </div>  
        <div class="linkbtn">
                 <a href="<?php echo strip_tags(get_field('storelink',$post->id)); ?>">
                        <?php echo $bookcode; ?>
                 </a>
        </div>

        <?php endforeach; wp_reset_postdata();?>    
    </div>        
</div>  
<?php endwhile; ?>
</article> 
share|improve this question
    
PHP foreach loop works for an array, but in your variable $custom_posts it's taking only one value with ACF's get_field(). To get all the custom fields you will need a WP_Query() with Custom Fields parameters –  Mayeenul Islam Apr 9 at 6:04
    
how...could u plz correct the code –  user3449454 Apr 9 at 6:24
    
Sorry, I assumed that the <div class="bookname"> was supposed to contain the name of the book, not the the name of the store. Could you please adjust the classes name in your HTML because it's hard to understand what goes where ... –  HamzStramGram Apr 9 at 14:56
    
done plz see above –  user3449454 Apr 9 at 16:45
    
where in your template are you trying to do this, and what comes before? if a field is returning nothing and it does indeed have a value, then the issue is likely that the underlying function is not getting the correct post ID from the global $post. get_field('storelist') assumes that global $post->ID contains the post ID of the post you are trying to fetch this data from. –  Milo Apr 9 at 17:16

1 Answer 1

I assume that the ACF you added to your post type "book" is a "Post Object" with the option "select multiple values" acitvated. So for each book you have, multiple stores can be linked to this one.

Here is some code that could help you :

<div class="bookdata">
    <?php
    // Get the linked stores
    // if the ACF is a type "Post Object" and the field is a list with a multiselect available
    $storelist = get_field('storelist');
    ?>
    <div class="bookname">
        <?php the_title();?>
    </div>
    <div class="bookcode">
        <?php the_field('bookcode');?>
    </div>
    <div class="stores-selling">
        <!-- We loop through all the store selected on the book post -->
        <?php foreach ($storelist as $post) :  setup_postdata($post); ?>
            <div class="clickbtn">
                <a href="<?php the_field('storeurl') ?>">
                    <?php the_title();?>
                </a>
            </div>
        <?php endforeach; wp_reset_postdata(); ?>
    </div>
</div>

I changed the HTML a little bit because I wasn't sure of what you exactly want to do. But the idea is there and it should do the job.

Please let me know if something isn't clear.

EDIT

I've maybe read your code too fast. You are doing a <?php get_field('storeurl'); ?> this function returns you the variable, but doesn't display it. Try with <?php the_field('storeurl'); ?> or <?php echo get_field('storeurl'); ?>

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.