I have 2 posttypes books and stores where stores is assigned as postobject for books.
bookcode and bookinfo are fields of posttype-book and storeurl is customfield of posttype-stores which is extracted based on the store selected in the storelist
in below all posts are displayed …with correct storename bookcode & bookinfo for all posts but the storeurl is extracted correctly only for the lastpost how do i get it to extract for all posts correctly & the bookinfo and the bookcode get repeated and
bookinfo and bookcode belong to posttype-books and storelist,storelink belong to posttype store.
store is assigned as postobject to books using advanced custom field plugin.
i am trying to display the store name and extract the corresponding storelink based on the store selected in the storelist for each post along with other fields of the post in the books
the bookinfo and the bookcode get repeated and storelink is not extracted except for the lastpost
<?php
while (have_posts()): the_post();?>
<div class="bookindex">
<div class="bookdata">
<?php
$storelist = get_field('storelist');
foreach ($storelist as $post) : setup_postdata($post);
$storename = get_the_title($post->id);
$storelink = strip_tags(get_field('storelink',$post->id));
//var_dump($storelink);
?>
<div class="storename">
<?php echo $storename;?>
</div>
<?php endforeach; wp_reset_postdata(); ?>
<?php
$bookinfo = get_field('bookinfo',$post->id);
?>
<div class="bookinfo">
<?php echo $bookinfo; ?>
</div>
<?php
$bookcode = get_field('bookcode',$post->id);
?>
<div class="clickbtn">
<a href="<?php echo $storelink; ?>">
<?php echo $bookcode; ?>
</a>
</div>
</div>
</div>
<?php endwhile; ?>
comeon …not a single reply
I have not dealt with WP but just based on what I see, and lining up <div> tags it looks like you’d want to move the endforeach line down to below where you want the link so that these values are contained within the foreach statement.
<?php
while (have_posts()): the_post();?>
<div class="bookindex">
<div class="bookdata">
<?php
$storelist = get_field('storelist');
foreach ($storelist as $post):
setup_postdata($post);
$storename = get_the_title($post->id);
$storelink = strip_tags(get_field('storelink',$post->id));
//var_dump($storelink);
?>
<div class="storename">
<?php echo $storename;?>
</div>
<?php
$bookinfo = get_field('bookinfo',$post->id);
?>
<div class="bookinfo">
<?php echo $bookinfo; ?>
</div>
<?php
$bookcode = get_field('bookcode',$post->id);
?>
<div class="clickbtn">
<a href="<?php echo $storelink; ?>"><?php echo $bookcode; ?></a>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div>
</div>
<?php endwhile; ?>
Slightly cleaner version.
<?php
while (have_posts()): the_post();?>
<div class="bookindex">
<div class="bookdata">
<?php
$storelist = get_field('storelist');
foreach ($storelist as $post):
setup_postdata($post);
$storename = get_the_title($post->id);
$storelink = strip_tags(get_field('storelink',$post->id));
$bookinfo = get_field('bookinfo',$post->id);
$bookcode = get_field('bookcode',$post->id);
//var_dump($storelink);
?>
<div class="storename">
<?php echo $storename;?>
</div>
<div class="bookinfo">
<?php echo $bookinfo; ?>
</div>
<div class="clickbtn">
<a href="<?php echo $storelink; ?>"><?php echo $bookcode; ?></a>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div>
</div>
<?php endwhile; ?>
nope not working it gives output as
What does not working mean? The image means nothing if I don’t know what lines are doing. Are you saying $bookcode = get_field(‘bookcode’,$post->id); is not returning a value? If you add this right after the bookcode line do you see an array with bookcode as key?
echo "<pre>";
print_r($post->id);
echo "</pre>";
the bookinfo and bookcode are not pulled out at all…they are empty…when i write print_r(post->id) below bookinfo it returns nothing…however when i put change the order of code as
$bookinfo = get_field('bookinfo',$post->id);
$bookcode = get_field('bookcode',$post->id);
$storelist = get_field('storelist');
i.e move bookcode and bookinfo above storelist
then bothe bookinfo and book code get displayed but get repeated more than once for each store but the storelink still doesn’t get extracted for all only last post
Well what’s the difference here?
$bookinfo = get_field('bookinfo',$post->id);
$bookcode = get_field('bookcode',$post->id);
$storelist = get_field('storelist');
Sure looks like you need to add the ,$post->id to me.
bookinfo and bookcode belong to posttype books which has a dropdown called storelist this storelist is populated from stores posttype with names in it.