I am using the code below in a page template.
I have a custom post type of BOOK and there are 2 categories, author and contributor.
My aim is to display the books in author category first, then below these the books in the contributor category.
I think there's a problem with the loop, as the books display fine, but the links don't work. Each of the contributor books displayed take the link from the last book of the author category rather than the one that is associated to itself.
I am using 'amazon_link' to retrieve the custom field of the same name
Any help, much appreciated!
Thanks Mark
<?php
/*
Template Name: Books
*/
?>
<?php get_header(); ?>
<div id="content">
<?php the_post_thumbnail( 'full' ); ?>
<header class="article-header-blue">
<div id="inner-content" class="row">
<div id="main" class="small-12 medium-12 large-12 columns" role="main">
<h1 class="page-title"><?php the_title(); ?></h1>
</div>
</div>
</header> <!-- end article header -->
<div id="inner-content" class="row" data-equalizer>
<?php $loop = new WP_Query( array( 'post_type' => 'books', 'category_name' => 'author', 'posts_per_page' => 12 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="small-12 medium-6 large-4 columns">
<div id="book" data-equalizer-watch>
<a href="<?php echo get_post_meta($post->ID, 'amazon_link', true); ?>" target="_blank">
<?php
// get an image field
$image = get_field('image');
// each image contains a custom field called 'link'
$amazon_link = get_field('amazon_link', $image['ID']);
// render
?>
<a href="<?php echo $amazon_link; ?>" target="_blank">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" width="200" class="book-img" />
</a>
</a>
<div class="book-container">
<h4 class="book"><a href="<?php echo $amazon_link; ?>" target="_blank"><?php the_title();?></a></h4>
<span class="year-published"><?php echo get_post_meta($post->ID, 'year_published', true); ?></span>
<div class="description-box">
<p class="description"><?php echo get_post_meta($post->ID, 'description', true); ?></p>
</div>
</div>
<a href="<?php echo get_post_meta($post->ID, 'amazon_link', true); ?>" target="blank" title="Buy on Amazon" class="button radius amazon">Buy on Amazon</a>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
<div id="inner-content" class="row" data-equalizer>
<h4 class="books">As contributor</h4>
<?php $loop = new WP_Query( array( 'post_type' => 'books', 'category_name' => 'contributor', 'posts_per_page' => 12 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="small-12 medium-6 large-4 columns">
<div id="book" data-equalizer-watch>
<?php
// get an image field
$image = get_field('image');
// each image contains a custom field called 'link'
$link = get_field('amazon_link', $image['ID']);
// render
?>
<a href="<?php echo $amazon_link; ?>" target="_blank">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" width="200" class="book-img" />
</a>
<div class="book-container">
<h4 class="book"><?php the_title();?></h4>
<span class="year-published"><?php echo get_post_meta($post->ID, 'year_published', true); ?></span>
<div class="description-box">
<p class="description"><?php echo get_post_meta($post->ID, 'description', true); ?></p>
</div>
</div>
<a href="<?php echo get_post_meta($post->ID, 'amazon_link', true); ?>" target="blank" title="Buy on Amazon" class="button radius amazon">Buy on Amazon</a>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
</div> <!-- end #content -->