【问题标题】:Post images printing on default blog page在默认博客页面上发布图像打印
【发布时间】:2013-01-30 02:21:22
【问题描述】:

当我在每篇博客文章中添加带有特色图片或高级服装字段的图片时,这些图片也会出现在我的默认博客页面 (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;?>

然后在 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;

非常感谢您的帮助。

【问题讨论】:

    标签: php wordpress loops blogs


    【解决方案1】:

    好的。标题中的代码循环遍历每个页面上显示的所有帖子。如果您只想在单个帖子页面上显示标题中的图像,则必须将其包装到条件 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;?>
    

    【讨论】:

    • 谢谢大卫,我明白你的意思,我都试过了,但它只是破坏了图像并且根本没有显示任何东西。任何想法这可能意味着什么?
    • 这会去除(中断)您首页上的图像,但会继续在单个帖子或页面上显示图像(取决于您的条件标签)。您还想防止在首页上显示图像吗?比你必须决定要显示什么图像。如果您想在首页显示第一篇或最后一篇文章的图片。
    猜你喜欢
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    相关资源
    最近更新 更多