当我在每篇博客文章中添加带有特征图像或高级服装字段的图像时,但图像也出现在我的默认博客页面上(index.php)。
所以我的博客有 4 篇文章。我的默认博客页面显示 4 篇博客文章,它还显示每篇博客文章的图像集。
这是我在显示图像的标题中:
if (have_posts()) : while (have_posts()) : the_post();
$attachment_id = get_field('banner_image');
if( get_field('banner_image') ):
$image = wp_get_attachment_image_src( $attachment_id, 'banner' );
?><img src="<?php echo $image[0]; ?>" alt="" width ="<?php echo $image[1]?>" height ="<?php echo $image[2]?>" /><?php
endif;
endwhile;
endif;?>
然后代码在索引中打印我所有的博客文章.php:
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="news-artical">
<?php the_title('<h1>', '</h1>');
the_excerpt();
//var_dump($post);
?> <hr>
</div> <?php
endwhile;
真的很感激一些帮助。
好的。标题中的代码循环遍历每个页面上显示的所有帖子。如果您只想在单个帖子页面上显示标题中的图像,则必须将其包装到条件中,如果 ( is_single() ) : 或 if ( is_singular() ) : 如果您想在单个页面上显示标题图像。
if ( is_single() && have_posts()) : while (have_posts()) : the_post();
$attachment_id = get_field('banner_image');
if( get_field('banner_image') ):
$image = wp_get_attachment_image_src( $attachment_id, 'banner' );
?><img src="<?php echo $image[0]; ?>" alt="" width ="<?php echo $image[1]?>" height ="<?php echo $image[2]?>" /><?php
endif;
endwhile;
endif;?>