我是wordpress的新手,并尝试更改代码,以便我可以获取图像源,因此我可以在css中将其用作背景但是生成图像
这是已经用funtions.php
编写的代码$string .= '<p class="recent-image"><a href="' . get_the_permalink() .'">' . get_the_post_thumbnail($post_id, array( 700, 500) ) .'</a></p></div>';
在审阅了许多关于此的帖子之后,我发现我只能通过以下代码获取图片网址
$string .= '<p class="recent-image"><a href="' . get_the_permalink() .'">' . wp_get_attachment_image_src() .'</a></p></div>';
在我的代码wp_get_attachment_image_src()
中更新此代码,但结果中没有生成任何内容。
有人可以建议我应该尝试一下吗?
答案 0 :(得分:1)
您可以使用get_the_post_thumbnail_url()
:
$string .= '<p class="recent-image"><a href="' . get_the_permalink() .'">' . get_the_post_thumbnail_url($post_id, array( 700, 500) ) .'</a></p></div>';
如果您想使用wp_get_attachment_image_src()
,还需要传递帖子ID:
wp_get_attachment_image_src($post_id)
$string .= '<p class="recent-image"><a href="' . get_the_permalink() .'">' . wp_get_attachment_image_src($post_id) .'</a></p></div>';
您可以阅读有关wp_get_attachment_image_src
here的文档,其中您可以看到传递给此函数的第一个参数是id
,它是必需的,而不是可选的,这就是它返回的原因没有像你用过的那样。
答案 1 :(得分:0)
<?php get_image_tag( $id, $alt, $title, $align, $size ); ?>