0

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 -->

2 Answers 2

0

The solution is simple: in the first loop, you are setting the $amazon_link like this:

$amazon_link = get_field('amazon_link', $image['ID']);

but in the second loop, you are setting a $link, like this:

$link = get_field('amazon_link', $image['ID']);

but you're still trying to echo $amazon_link variable later on, which has never been setup in the second loop, so it's value is still the value of the last book from the first loop.

1
  • @MarkJenkins can you mark the answer as approved? Commented Nov 1, 2015 at 16:37
-2

you need to use get_post_meta() not get_field()

2
  • 1
    He is probably using Advanced Custom Fields, where you get the field's value using get_field() function. Commented Nov 1, 2015 at 16:08
  • I actually stuffed up myself, I wasn't doing the <a> tag correctly. Fixed it, now works. Commented Nov 1, 2015 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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