【发布时间】:2020-11-02 12:39:12
【问题描述】:
我已经为这个问题挠头一两天了。我试图让 WordPress 使用调用functions.php 中的函数的简码打印所有最近的帖子。我设法让代码工作,但它打印到页面顶部,因为我认为 PHP echos 默认情况下我需要 return。另一个问题是目前它只打印一个最近的结果。在我开始使用 HEREDOC 之前,循环正在工作,但我认为我需要使用它来返回而不是回显。
代码:
add_shortcode('recentvideos' , 'printrecenttv');
function printrecenttv(){
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 4, // Number of recent posts thumbnails to display
'post_status' => 'publish', // Show only the published posts
'post_type' => "tv" //Show only Videos
));
foreach($recent_posts as $post) :
$perm = get_permalink($post['ID']);
$imgurl = get_the_post_thumbnail_url($post['ID'], 'full');
return <<<HTML
<div class="videoposter">
<a class="posterlink" href="$perm">
<img class="posterimg" src="$imgurl">
</a>
</div>
HTML;
endforeach; wp_reset_query();
}
我做错了什么?
【问题讨论】:
标签: php wordpress foreach heredoc