Php Wordpress类别页未正确返回数组
我正在编辑我的分类页面。使用一些自定义字段,我定义了一个图像。对于一个类别中的每一篇文章,我想将这个自定义图像添加到一个数组中,我将该数组转换为一个图像库。我使用下面的代码,但由于某种原因,当涉及到内爆数组时,我得到的只是一个图像(对应于加载的最后一篇文章)。我肯定可能是我放错地方了,但我就是想不出来。任何帮助都将不胜感激Php Wordpress类别页未正确返回数组,php,arrays,wordpress,wordpress-theming,Php,Arrays,Wordpress,Wordpress Theming,我正在编辑我的分类页面。使用一些自定义字段,我定义了一个图像。对于一个类别中的每一篇文章,我想将这个自定义图像添加到一个数组中,我将该数组转换为一个图像库。我使用下面的代码,但由于某种原因,当涉及到内爆数组时,我得到的只是一个图像(对应于加载的最后一篇文章)。我肯定可能是我放错地方了,但我就是想不出来。任何帮助都将不胜感激 <?php if ( have_posts() ) : while ( have_posts() ) : the_post();
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$gallery_images = get_custom_field('catImage:to_array');
$thumbs_html = array();
foreach ($gallery_images as $galleryID) {
$attachment = get_post( $galleryID );
$description = get_custom_field('catIntro:do_shortcode'); //get pages introductory text
$caption = get_the_title(); //get page title
$button_html .= '<div id="description-button-' . $gallery_images_count . '" class="show-description-button">Show Caption</div>';
$description_html .= '<div id="description-' . $gallery_images_count . '" class="photoDescription" style="display:none;">' . $description . '</div>';
$caption_html .= '<div id="caption-' . $gallery_images_count . '" class="photoCaption" style="display:none;">' . $caption . '</div>';
$thumb_img = wp_get_attachment_image_src( $galleryID, 'thumbnail' ); //thumbnail src
$full_img = wp_get_attachment_image_src( $galleryID, 'full' ); //full img src
$thumbs_html[] = '<div class="indvlThumbCont"><a href="' . $full_img[0] . '" id="description-button-' . $gallery_images_count . '" class="thumbLink" target="_blank"><img class="thumbImg" src="' . $thumb_img[0] .'"></a></div>';
$gallery_images_count++;
}//end forEach
//calculate the width of the thumbar
$widthPerImg = 157;
$thumbBarWidth = $gallery_images_count * $widthPerImg;
print $gallery_images_count;
?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
<div id="thumbsBar">
<div id="left" class="scrollCtrl left desktopOnly"><span></span></div>
<div class="toHideScrollBar" id="toHideScrollBar">
<div class="moveCanvas" style="width:<?php echo $thumbBarWidth; ?>px;">
<?php echo implode("\n", $thumbs_html); ?>
</div>
</div>
<div id="right" class="scrollCtrl right desktopOnly"><span></span></div>
<span id="total_number_in_gallery " class="<?php echo $gallery_images_count; ?>"></span>
</div>
如果您使用的主题类似于Twenty12(默认情况下,分类页面上只显示一篇文章),那么这就是问题所在。您可以通过(如果您对修改主循环没有意见的话)在代码之前添加以下内容来解决此问题:
query_posts('showposts=y&cat=x');
谢谢你的建议,我已经做了更多的挖掘,似乎因为我每次都设置数组,而不是在末尾添加项目(array_push),所以我只记录了一个值。