Reputation: 2991
I'm trying to loop through all categories one by one, and printing out the post title/image/image-link. How can I make my code work?
<?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>';
}
}
}
?>
I know my echo statements are probably wrong in the second foreach loop, but it's absolutely important the first for loops stays the same/where it is. Please help.
Upvotes: 0
Views: 269
Reputation: 1816
So...
Try this:
<?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];
}
}
}
?>
Upvotes: 1