Php 在默认博客页面上发布图像打印

Php 在默认博客页面上发布图像打印,php,wordpress,loops,blogs,Php,Wordpress,Loops,Blogs,当我在每个博客文章中添加一个带有feature image或advanced Costment字段的图像时,这些图像也会出现在我的默认博客页面(index.php)上 所以我的博客有4篇文章。我的默认博客页面显示了4篇博客文章,它还显示了每个博客文章的图像集 下面是显示图像的标题中的my: if (have_posts()) : while (have_posts()) : the_post(); $attachment_id = get_field('banner_image'); if(

当我在每个博客文章中添加一个带有feature image或advanced Costment字段的图像时,这些图像也会出现在我的默认博客页面(index.php)上

所以我的博客有4篇文章。我的默认博客页面显示了4篇博客文章,它还显示了每个博客文章的图像集

下面是显示图像的标题中的my:

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;?>
if(have_posts()):while(have_posts()):the_post();
$attachment_id=get_字段('banner_image');
如果(获取_字段('banner_image')):
$image=wp_get_attachment_image_src($attachment_id,'banner');
?>“alt=”“width=”“height=”“/>
然后在index.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;
if(have_posts()):while(have_posts()):the_post();?>


好的。标题中的代码在每页上显示的所有文章中循环。如果要仅在单个文章页上显示标题中的图像,则必须将其包装为条件If(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;?>
if(is_single()&&have_posts()):while(have_posts()):the_post();
$attachment_id=get_字段('banner_image');
如果(获取_字段('banner_image')):
$image=wp_get_attachment_image_src($attachment_id,'banner');
?>“alt=”“width=”“height=”“/>

谢谢David,我明白你的意思,我已经尝试了这两种方法,但这只会破坏图像,根本没有显示任何内容。你知道这意味着什么吗?这会去掉(打断)你头版上的图片,但继续在单个帖子或页面上显示图片(取决于你的条件标签)。您想阻止在头版上也显示图像吗?然后你必须决定要展示什么样的图像。若你们想显示第一篇或最后一篇文章的图片,请在头版收听。