我是PHP新手,我想创建一个代码,其中显示最新帖子和帖子标题的缩略图。标题和图像都应位于<a href="#">
内,以便人们可以通过单击图像来查看文章。但是,当我运行以下PHP代码时,代码将被打印出来:
<img width="462" height="260" src="http://vocaloid.de/wp-content/uploads/2014/11/1920x1080_PC_a.jpg" class="attachment-735x260 wp-post-image" alt="1920x1080_PC_a"><a href="http://vocaloid.de/news/test-nr-2/"><h2>HATSUNE MIKU: PROJECT DIVA F EXTEND ANGEKÜNDG</h2></a>
&#13;
以下是我使用的原始代码:
<?php
$args = array('numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<a href="' . get_permalink($recent["ID"]) . '">' . the_post_thumbnail($recent['ID'], array(735,260)); the_title( '<h2>', '</h2>' ); '</a>';
}
?>
答案 0 :(得分:1)
试试这个:
$args = array('numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<a href="' . get_permalink($recent["ID"]) . '">';
the_post_thumbnail($recent['ID'], array(735,260));
echo the_title( '<h2>', '</h2>' ).'</a>';
}
让我知道输出。
答案 1 :(得分:0)
Rohil_PHPBeginner的回答是正确的,但我不认为它解释了为什么会这样。
使用the_post_thumbnail()
的地方,它会回显结果而不会返回结果。因此,当您尝试将其连接成字符串时,它出现在错误的位置。
如果您想获得帖子缩略图,可以使用get_the_post_thumbnail()
因此,使用get_the_post_thumbnail()
您的代码段最终会显示为:
<?php
$args = array('numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<a href="' . get_permalink($recent["ID"]) . '">'
. get_the_post_thumbnail($recent['ID'], array(735,260))
. the_title( '<h2>', '</h2>' )
. '</a>';
}
?>
我希望这进一步明确。