我正在尝试逐个遍历所有类别,并打印出帖子标题/图像/图像链接。如何使我的代码工作?
<?php $categories= get_categories();
foreach ($categories as $cat) {
echo '<div>'.
$posts = get_posts(array('category' => $cat->term_id));
if ($posts) {
foreach ($posts as $p) {
echo get_the_post_title( $p->title ).'<br>';
echo get_the_post_thumbnail( $p->ID, 'medium' ).'<br>';
echo get_the_post_thumbnail_link( $p->imagelink, 'medium' ).'<br>';
}
}
}
?>
我知道我的echo语句在第二个foreach循环中可能是错误的,但绝对重要的是第一个循环保持不变/它在哪里。请帮忙。
答案 0 :(得分:1)
因此...
试试这个:
<?php $categories= get_categories();
foreach ($categories as $cat) {
echo '<div>';
$posts = get_posts(array('category' => $cat->term_id));
if ($posts) {
foreach ($posts as $p) {
echo get_the_title( $p->ID ).'<br>';
echo get_the_post_thumbnail( $p->ID, 'medium' ).'<br>';
echo wp_get_attachment_image_src( get_post_thumbnail_id($p->ID), 'medium' )[0];
}
}
}
?>