有一个自定义帖子类型,其中每个帖子都是一个图库,每个帖子都有一个高级自定义字段图库字段,每个字段都有5-20个图像。还有一个自定义的post类型post模板,如下所示(去掉大部分代码,重点关注与此相关的部分。)代码中的注释解释了每个部分的作用。总的来说,它似乎对所有工作都有效,但无论我在这个自定义帖子类型中的哪个帖子,例如示例。com/gallery/gallery-1,示例。com/gallery/gallery-2,示例。com/gallery/gallery-3等。它在每个页面上显示相同的特色图像和缩略图。它似乎是从第一篇文章中提取第一个库,而不是在每个页面上检查以提取与该页面关联的库项目。
我相信这很简单,比如需要刷新/重置一个循环之类的事情,但是很难找到正确的描述方法来在谷歌上找到答案。本来是这样的,但一旦我不得不在循环中添加循环,问题就开始了。如果有人有任何想法,那就太好了。同时将继续研究。
<?php get_header(); ?>
<div id="main">
<?php
//Checks gallery custom post type and finds the current pages entry
$args = array( \'post_type\' => \'customers_gallery\', \'posts_per_page\' => 1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div>
<?php
//Looks up the advanced custom field gallery associated with this post
$hero = get_field(\'gallery_images\'); ?>
<!-- //Show the first image in that gallery -->
<img src="<?php echo $hero[0][\'url\']; ?>" class="main-image" style="width: 484px;">
<!-- //Shows title of this page -->
<h3><?php the_title(); ?></h3>
<?php
//Looks up the advanced custom field gallery associated with this post
$images = get_field(\'gallery_images\');
//Loops through all the gallery image showing a thumbnail for each and each is linked to its full size
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<a href="<?php echo $image[\'url\']; ?>">
<img src="<?php echo $image[\'sizes\'][\'gallery-landscape-thb\']; ?>" alt="<?php echo $image[\'alt\']; ?>" />
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<div>
<ul>
<?php
//Looks up all the pages in this custom post type and lists the titles of the first 12 and linked to respective pages
$custom_query = new WP_Query(
array(
\'post_type\' => \'customers_gallery\',
\'posts_per_page\' => 12
)
);
if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; endif; wp_reset_query(); ?>
</ul>
</div>
</div>
<?php endwhile; ?>
</div>
<!-- /#main -->
<?php get_footer(); ?>
更新有些人注意到要添加重置,但我仍然认为问题是其他的。因为我可以删除模板中几乎所有的代码,并将其下载到以下内容,无论我在库自定义帖子类型中查看哪个帖子,它都会回显相同的标题(它显示了库自定义帖子类型中帖子列表中的第一个标题,它应该在哪里拉入特定页面的标题。选项卡中的标题是正确的。
<?php get_header(); ?>
<?php
$args = array( \'post_type\' => \'customers_gallery\', \'posts_per_page\' => 1 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php echo the_title(); ?>
<?php endwhile; ?>
<?php get_footer(); ?>
更新2返回到我在这条消息中发布的原始代码,并简单地删除了我在第二篇文章aka中提到的代码,$args=array(\'post\\u type=>\'customers\\u gallery\'…所有这些都可以使用。因此,问题似乎是我试图为自定义帖子类型创建一个循环,但在使用单个-[自定义帖子类型名称]php时,似乎不需要这样做。