Php 如何将图片添加到主页?
我正试图在我的WordPress主页上制作一个mod。我有以下代码(index.php):Php 如何将图片添加到主页?,php,wordpress,Php,Wordpress,我正试图在我的WordPress主页上制作一个mod。我有以下代码(index.php): 不幸的是,这会显示部分或全部文章内容,但不会显示附加的图像。如何更改以显示它们?我不想使用特色图片,只想使用附加的图片。通过研究您的问题,我发现了一个新的Wordpress功能。重要的是要知道,有很多不同的方法来实现您所要求的,而将下面的代码保留在我下面插入的表单中并不理想。这只应被视为一个起点 最佳做法是将其移动到content.php文件中(正如@Nilambar在对您的问题的评论中提到的)。甚至
不幸的是,这会显示部分或全部文章内容,但不会显示附加的图像。如何更改以显示它们?我不想使用特色图片,只想使用附加的图片。通过研究您的问题,我发现了一个新的Wordpress功能。重要的是要知道,有很多不同的方法来实现您所要求的,而将下面的代码保留在我下面插入的表单中并不理想。这只应被视为一个起点 最佳做法是将其移动到
content.php
文件中(正如@Nilambar在对您的问题的评论中提到的)。甚至可以创建一个新文件,方法是复制content.php
并调用content-attached-media.php
,然后将get\u-template\u-part
行更改为get\u-template\u-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; ?>
使用的文件
你用的是哪个主题<上面的代码中包含code>content.php。你可以在那里查一下。
<?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; ?>