我正在页面模板中使用下面的代码。
我有一个自定义的帖子类型的书,有两个类别,作者和贡献者。
我的目标是首先显示“作者”类别中的图书,然后在这些图书下面显示“贡献者”类别中的图书。
我认为循环有问题,因为书籍显示良好,但链接不起作用。显示的每本贡献者书籍都从作者类别的最后一本书中获取链接,而不是从与自身关联的那本书中获取链接。
我正在使用“amazon\\u链接”检索同名的自定义字段
任何帮助,非常感谢!
感恩节标志
<?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 -->