WordPress.org

Forums

Loop inside sidebar? (3 posts)

  1. kab2512
    Member
    Posted 4 years ago #

    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;
  2. stvwlf
    Member
    Posted 4 years ago #

    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;
    ?>
  3. kab2512
    Member
    Posted 4 years ago #

    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;
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic