我试图在我的WordPress主页上制作一个mod。我有这个代码(index.php):
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content'); ?>
<?php endwhile; ?>
不幸的是,这会显示部分或全部帖子内容,但不显示附加图片。如何更改它才能显示它们?我不想使用特色图片,只想使用附加图片。
答案 0 :(得分: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