Support » Fixing WordPress » Loop inside sidebar?

  • Is it ok to make a loop inside a sidebar?
    Or should it do otherwise?

    <?php
    
    while ( have_posts() ) : the_post(); 
    
    	$post_id = $wp_query->post->ID;
    
    $random = get_post_meta($post_id, 'random', true) ?> 
    
          <?php echo $random; ?> 
    
    <?php endwhile;
Viewing 2 replies - 1 through 2 (of 2 total)
  • better to use a custom query loop

    <?php
      $sidebarPosts = new WP_Query();
      $sidebarPosts->query('posts_per_page=5&cat=3,7,12');
      while ($sidebarPosts->have_posts()) : $sidebarPosts->the_post(); ?>
         $post_id = $wp_query->post->ID;
         $random = get_post_meta($post_id, 'random', true);
         echo $random;
      endwhile;
    ?>

    Thank you for your answer. Very helpful

    Shouldnt it be

    <?php
      $sidebarPosts = new WP_Query();
      $sidebarPosts->query('posts_per_page=5&cat=3,7,12');
      while ($sidebarPosts->have_posts()) : $sidebarPosts->the_post(); ?>
     $post_id = $sidebarPosts->post->ID
     $random = get_post_meta($post_id, 'random', true);
     echo $random;
      endwhile;
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Loop inside sidebar?’ is closed to new replies.