Loop not working
-
Ok I want to call the loop in the sidebar and run a custom function on each post ID.
Currently when I call the function it loops through all the posts and will echo every post number, but the image it displays is the first image everytime.
I echoed out $post->ID and it returns the correct post ID for each loop, just the wrong image.
There’s the get image function in case there’s an issue with that.
Sidebar code:
mdv_recent_posts();
function mdv_recent_posts($no_posts = 5, $before = ‘- ‘, $after = ‘
‘, $hide_pass_post = true, $skip_posts = 0, $show_excerpts = false, $include_pages = false) {
global $wpdb;
$time_difference = get_settings(‘gmt_offset’);
$now = gmdate(“Y-m-d H:i:s”,time());
$request = “SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = ‘publish’ “;
if($hide_pass_post) $request .= “AND post_password =” “;
if($include_pages) $request .= “AND (post_type=’post’ OR post_type=’page’) “;
else $request .= “AND post_type=’post’ “;
$request .= “AND post_date_gmt < ‘$now’ ORDER BY post_date DESC LIMIT $skip_posts, $no_posts”;
$posts = $wpdb->get_results($request);
$output = ”;
if($posts) {
foreach ($posts as $post) {
echo get_the_image( ‘post_id=’ . $post->ID );
echo $post->ID;
}
} else {
$output .= $before . “None found” . $after;
}The output can be found on the right sidebar:
- The topic ‘Loop not working’ is closed to new replies.