我正在为WordPress创建自定义模板。我可以在一个帖子中添加多个精选图像,并可以将它们显示为图库或滑块。 第一篇文章一切正常。
当我添加2个或更多精选图像的第二篇文章时,如果我只显示1个精选图像,则根本不会显示任何图像。
任何人都可以查看我的代码并告诉我我错过了什么:
修改
<?php /* Template Name: Home v4 */ get_template_part('/template-parts/home4_header'); ?>
<section class="background-color">
<?php query_posts('showposts=0'); ?>
<?php $i = 1; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if(($i % 4) !== 0) { ?>
<!-- Fixed width banner -->
<div class="container">
<div class="containerBox">
<!-- Multiple Featured Images Slider -->
<?php
if (class_exists('MultiPostThumbnails')) {
// Loops through each feature image and grabs thumbnail URL
$x=1; $image1_id; $image2_id; $image3_id; $image4_id; $image5_id;
while ($x<=5) {
$image_name = 'feature-image-'.$x; // sets image name as feature-image-1, feature-image-2 etc.
if (MultiPostThumbnails::has_post_thumbnail('post', $image_name)) {
$image_id = MultiPostThumbnails::get_post_thumbnail_id( 'post', $image_name, $post->ID ); // use the MultiPostThumbnails to get the image ID
$image_thumb_url = wp_get_attachment_image_src( $image_id,'small-thumb'); // define thumb src based on image ID
$image_feature_url = wp_get_attachment_image_src( $image_id,'feature-image' ); // define full size src based on image ID
$attr = array(
'rel' => $image_thumb_url[0], // sets the url for the image thumbnails size
'src' => $image_feature_url[0], // sets the url for the full image size
);
// Use wp_get_attachment_image instead of standard MultiPostThumbnails to be able to tweak attributes
$image = wp_get_attachment_image( $image_id, 'feature-image', false, $attr );
//echo $image;
if ($x == 1) {$image1_id = $image_id;}
if ($x == 2) {$image2_id = $image_id;}
if ($x == 3) {$image3_id = $image_id;}
if ($x == 4) {$image4_id = $image_id;}
if ($x == 5) {$image5_id = $image_id;}
}
$x++;
}
}; // end if MultiPostThumbnails
if (!empty($image2_id)) { ?>
<div class="multi-thumb">
<div id="ninja-slider">
<div class="slider-inner">
<ul>
<li>
<div class="slider">
<?php the_post_thumbnail('full'); ?>
</div>
</li>
<li>
<div class="slider">
<img src="<?php echo wp_get_attachment_image( $image2_id, 'feature-image', false, $attr );?>">
</div>
</li>
<?php if (!empty($image3_id)) { ?>
<li>
<div class="slider">
<img src="<?php echo wp_get_attachment_image( $image3_id, 'feature-image', false, $attr );?>">
</div>
</li>
<?php } ?>
<?php if (!empty($image4_id)) { ?>
<li>
<div class="slider">
<img src="<?php echo wp_get_attachment_image( $image4_id, 'feature-image', false, $attr );?>">
</div>
</li>
<?php } ?>
<?php if (!empty($image5_id)) { ?>
<li>
<div class="slider">
<img src="<?php echo wp_get_attachment_image( $image5_id, 'feature-image', false, $attr );?>">
</div>
</li>
<?php } ?>
</ul>
<div class="fs-icon" title="Expand/Close"></div>
</div>
</div>
</div>
<?php
unset($image1_id, $image2_id, $image3_id, $image4_id, $image5_id, $image_name, $image, $image_thumb_url, $image_feature_url);
} else { the_post_thumbnail('full'); } ?>
<!-- End Multiple Featured Images Slider -->
</div>
<div class="post-area-category container-post-color bottom-padding">
<div class="content-block w-clearfix">
<div class="category">
<h5><?php echo the_category();?></h5>
</div>
<h1 class="black mastheading-post-v3"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
<div class="date-block-v3 w-container">
<div class="black mini-date"><?php echo get_the_date(); ?></div>
<img class="mini-icon" src="<?php echo get_template_directory_uri() . '/images/comment-icon.png' ?>">
<div class="black mini-date">
<?php
$comments_count = wp_count_comments();
echo $comments_count->approved;
?>
</div>
<img class="mini-icon" src="<?php echo get_template_directory_uri() . '/images/heart-icon.png' ?>">
<div class="black mini-date">14</div>
</div>
<div class="black iltalica"><?php the_excerpt(); ?></div>
<a class="a8 cta-home-category w-button" href="<?php the_permalink(); ?>">continue reading</a>
</div>
</div>
<div class="space-4 background-color"></div>
</div>
<!-- Fixed width banner -->
<?php } elseif (($i % 4) == 0) { ?>
<!-- Full width banner -->
<div class="container-fluid">
<div class="containerBox">
<div class="home-v3-full-banner">
<?php the_post_thumbnail('full'); ?>
</div>
</div>
</div>
<div class="container post-area-category container-post-color bottom-padding">
<div class="content-block w-clearfix">
<div class="category">
<h5><?php echo the_category();?></h5>
</div>
<h1 class="black mastheading-post-v3"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
<div class="date-block-v3 w-container">
<div class="black mini-date"><?php echo get_the_date(); ?></div>
<img class="mini-icon" src="<?php echo get_template_directory_uri() . '/images/comment-icon.png' ?>">
<div class="black mini-date">
<?php
$comments_count = wp_count_comments();
echo $comments_count->approved;
?>
</div>
<img class="mini-icon" src="<?php echo get_template_directory_uri() . '/images/heart-icon.png' ?>">
<div class="black mini-date">14</div>
</div>
<div class="black iltalica"><?php the_excerpt(); ?></div>
<a class="a8 cta-home-category w-button" href="<?php the_permalink(); ?>">continue reading</a>
</div>
</div>
</div>
<!-- End Full width banner -->
<?php } ?>
<?php $i++; ?>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</section>
<?php get_footer(); ?>
答案 0 :(得分:0)
试试这段代码。未经测试
<?php query_posts('showposts=0'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $i = 1; ?>