为什么在 WordPress 中忽略 PHP 代码的顺序?
Why is the order of PHP code ignored in WordPress?
我是 PHP 的新手,我想创建一个代码来显示最新 post 的缩略图和 post 标题。标题和图片都应该在<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>
这是我使用的原始代码:
<?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>';
}
?>
试试这个:
$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>';
}
让我知道输出。
Rohil_PHPBeginner的回答是正确的,但我不认为它解释了它发生的原因。
在您使用 the_post_thumbnail()
的地方,它会回显结果而不是 return。正是出于这个原因,当您尝试将它连接成一个字符串时,它出现在错误的位置。
如果您想获取 post 缩略图,可以使用 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>';
}
?>
我希望这能进一步说明问题。
我是 PHP 的新手,我想创建一个代码来显示最新 post 的缩略图和 post 标题。标题和图片都应该在<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>
这是我使用的原始代码:
<?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>';
}
?>
试试这个:
$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>';
}
让我知道输出。
Rohil_PHPBeginner的回答是正确的,但我不认为它解释了它发生的原因。
在您使用 the_post_thumbnail()
的地方,它会回显结果而不是 return。正是出于这个原因,当您尝试将它连接成一个字符串时,它出现在错误的位置。
如果您想获取 post 缩略图,可以使用 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>';
}
?>
我希望这能进一步说明问题。