我正在尝试在我的WordPress主页上创建一个模块。我有这段代码(index.php):
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content'); ?>
<?php endwhile; ?>
不幸的是,这将显示部分或全部的文章内容,但不包括附加的图片。我该如何更改以便显示它们?我不想使用特色图片,而只想使用附加的那些。
通过研究您的问题,我实际上发现了一个新的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; ?>
content.php
被包含在上面的代码中。你可以在那里检查它。 - Nilambar Sharma