WordPress:如何将图片添加到主页?

3

我正在尝试在我的WordPress主页上创建一个模块。我有这段代码(index.php):

<?php while ( have_posts() ) : the_post(); ?>
    <?php get_template_part( 'content'); ?>
<?php endwhile; ?>

不幸的是,这将显示部分或全部的文章内容,但不包括附加的图片。我该如何更改以便显示它们?我不想使用特色图片,而只想使用附加的那些。


2
你使用的是哪个主题?content.php被包含在上面的代码中。你可以在那里检查它。 - Nilambar Sharma
1个回答

0

通过研究您的问题,我实际上发现了一个新的WordPress函数。重要的是要知道,有很多不同的方法来实现您所要求的内容,并且将以下代码保留在我插入下面的表单中并不理想。这只应该被视为一个起点。

最佳实践是将其移动到content.php文件中(正如@Nilambar在您的问题评论中提到的)。甚至可以通过复制content.php并将其命名为content-attached-media.php来创建一个新文件,并使用它通过将get_template_part行更改为get_template_part('content','attached-media')

以下是让您开始的代码:

<?php while ( have_posts() ) : the_post(); ?>
    <?php get_template_part( 'content'); ?>
    <?php
        /*
         * Choose what size the image should be displayed in by setting the $size variable.
         * Some options include 'thumbnail', 'medium', 'large', 'full'
        */
        $size = 'full';
        $attached_images = get_attached_media( 'image', get_the_ID() );
        foreach( $attached_images as $image ) {
            echo wp_get_attachment_image( $image->ID, $size );
        }
    ?>
<?php endwhile; ?>

文档使用: https://codex.wordpress.org/Function_Reference/get_attached_media https://codex.wordpress.org/Function_Reference/wp_get_attachment_image https://codex.wordpress.org/Function_Reference/the_post_thumbnail

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接