Title: Two Loops Problem
Last modified: August 21, 2016

---

# Two Loops Problem

 *  [Begin](https://wordpress.org/support/users/bentalgad/)
 * (@bentalgad)
 * [12 years, 5 months ago](https://wordpress.org/support/topic/two-loops-problem/)
 * Hi,
 * I have a ctegory page template which is build in this way:
 * First Loop = Posts from the current category which have the tag 233
 * Second Loop = Posts from the current category which don’t have the tag 233
 * The first problem is that the first loop is appearing at the top of all pages(
   instead of just the first one).
 * The second problem is that it suppose to give me 5 posts in every page and it
   does, but aside from the first loop that is just being add to those 5…
 * Here is the code:
 *     ```
       <?php get_header(); ?>
   
       <div id="morim_warper">
   
               <div class="header">
               <h1>
               <?php echo(get_category_parents($cat, TRUE, '&nbsp;')); ?>
       		</h1>
       		<span id="orderBy">לפי סדר א׳-ב׳<br/><span>פרט לקידום ממומן</span>
       		</span>
   
               </div>
       		<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
       		<br/>
       		<span id="startPageNumbers">
   
       		<?php
       		 	$categories = get_the_category();
       				$category_id = $categories[0]->cat_ID;
       			//	echo $category_id; // enable this to check what category this is in
       		?>
   
       		<? $the_query = new WP_Query( 'tag_id=233' ); ?>
       		<?php if ( $the_query->have_posts() ) {
       		        echo '<ul>';
       			while ( $the_query->have_posts() ) {
       				$the_query->the_post();
       				if ( in_category( $category_id )) { ?>
   
       					<div class="more">
       							<?php if( has_tag() ) { echo '<img class="starMark" src="http://www.guitara.co.il/wp-content/themes/mguitara/images/starMark.png"/>'; } else { echo ''; } ?>
       							<a href="<?php the_permalink() ?>" rel="bookmark" title="עוד פרטים על:
       							<?php the_title_attribute();?>">
       							<img class="teacherIMG" alt="מורה לגיטרה <?php the_title(); ?>"
       							src="http://www.guitara.co.il/images/teachers/<?php echo get_post_meta($post->ID, 'face', true); ?>_teacher.jpg"/>
       							</a>
   
       							<h1>
       							<a href="<?php the_permalink() ?>" rel="bookmark" title="עוד פרטים על:
       							<?php the_title_attribute();?>">
       							<?php the_title(); ?>
       							</a>
       							</h1>
   
       					        <p class="fewlines">
       					        <a href="<?php the_permalink() ?>" rel="bookmark" title="עוד פרטים על:
       							<?php the_title_attribute();?>">
       							<?php if ( ! has_excerpt() ) {
       					            $experience = get_post_meta($post->ID, 'experience', true);
       					            $guitars = get_post_meta($post->ID, 'guitars', true);
       					            $singlesentence = get_post_meta($post->ID, 'singlesentence', true);
       					            $birthDate =array();
       					$birthDate[] = get_post_meta($post->ID, 'birth_month', true);
       					$birthDate[] = get_post_meta($post->ID, 'birth_day', true);
       					$birthDate[] = get_post_meta($post->ID, 'birth_year', true);
       					$birthDate = array_filter($birthDate);
       					//get age from date or birthdate
       					if(!empty($birthDate)) {
       					$age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("Y")-$birthDate[2])-1):(date("Y")-$birthDate[2]));}
   
       					            echo "מורה לגיטרה $guitars, עם נסיון של $experience שנים. גיל:$age.            $singlesentence.
       					            ";
       					            } else {
       					            the_excerpt();
       					            }?>
       							</a>
       							</p>
   
       							<h2>
       							<b>מחיר שיעור:</b>
       							<?php echo get_post_meta($post->ID, "price", true);?> ₪
       							</h2>
       							<h2>
       							<b>טלפון:</b>
       							<?php echo get_post_meta($post->ID, "area_code", true);?>-<?php echo get_post_meta($post->ID, "phone", true);?>
       							</h2>
   
       					</div><!-- end more -->
   
       				<? } else {
       					// It's not in the category, ignore it
       				}
       			}
       		        echo '</ul>';
       		} else {
       			// no posts found
       			echo '';
       		}
       		?>
   
       		<?php rewind_posts(); ?>
   
       		<?php if (have_posts()) :?>
       		<?php $posts=query_posts($query_string . '&orderby=title&order=asc&tag__not_in=233');
       		      while (have_posts()) : the_post(); ?>
   
       <div class="more">
       		<?php if( has_tag() ) { echo '<img class="starMark" src="http://www.guitara.co.il/wp-content/themes/mguitara/images/starMark.png"/>'; } else { echo ''; } ?>
       		<a href="<?php the_permalink() ?>" rel="bookmark" title="עוד פרטים על:
       		<?php the_title_attribute();?>">
       		<img class="teacherIMG" alt="מורה לגיטרה <?php the_title(); ?>"
       		src="http://www.guitara.co.il/images/teachers/<?php echo get_post_meta($post->ID, 'face', true); ?>_teacher.jpg"/>
       		</a>
   
       		<h1>
       		<a href="<?php the_permalink() ?>" rel="bookmark" title="עוד פרטים על:
       		<?php the_title_attribute();?>">
       		<?php the_title(); ?>
       		</a>
       		</h1>
   
               <p class="fewlines">
               <a href="<?php the_permalink() ?>" rel="bookmark" title="עוד פרטים על:
       		<?php the_title_attribute();?>">
       		<?php if ( ! has_excerpt() ) {
                   $experience = get_post_meta($post->ID, 'experience', true);
                   $guitars = get_post_meta($post->ID, 'guitars', true);
                   $singlesentence = get_post_meta($post->ID, 'singlesentence', true);
                   $birthDate =array();
       $birthDate[] = get_post_meta($post->ID, 'birth_month', true);
       $birthDate[] = get_post_meta($post->ID, 'birth_day', true);
       $birthDate[] = get_post_meta($post->ID, 'birth_year', true);
       $birthDate = array_filter($birthDate);
       //get age from date or birthdate
       if(!empty($birthDate)) {
       $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md") ? ((date("Y")-$birthDate[2])-1):(date("Y")-$birthDate[2]));}
   
                   echo "מורה לגיטרה $guitars, עם נסיון של $experience שנים. גיל:$age.            $singlesentence.
                   ";
                   } else {
                   the_excerpt();
                   }?>
       		</a>
       		</p>
   
       		<h2>
       		<b>מחיר שיעור:</b>
       		<?php echo get_post_meta($post->ID, "price", true);?> ₪
       		</h2>
       		<h2>
       		<b>טלפון:</b>
       		<?php echo get_post_meta($post->ID, "area_code", true);?>-<?php echo get_post_meta($post->ID, "phone", true);?>
       		</h2>
   
       </div><!-- end more -->
       		<?php endwhile; ?>
       		</span><!-- EndPageNumbers -->
       		<?php if(function_exists('wp_page_numbers')) { wp_page_numbers(); } ?>
       </div><!-- end morim_warper -->		
   
       <div id="citylist">
       <h2>
       מצא מורה גיטרה (לפי עיר):
       </h2>
       <?php wp_list_categories('child_of=9&title_li='); ?>
       <div id="clear"></div>
       </div><!-- end #citylist -->
   
       <div id="wordsforcityofteachers">
       בעמוד הזה תוכלו למצוא מורה לגיטרה <?php echo get_cat_name( $cat ); ?> בין מגוון מורים <?php echo get_cat_name( $cat ); ?> ותוכלו לבחור מבין המורים הטובים ביותר <?php echo get_cat_name( $cat ); ?>. אם תחפשו מורה לגיטרה <?php echo get_cat_name( $cat ); ?> ולא תמצאו אתם מוזמנים לכתוב לנו בעמוד "צרו קשר" שבתפריט העליון. גם אם אתם יודעים על מורה לגיטרה <?php echo get_cat_name( $cat ); ?> שלא מופיע בעמוד זה נשמח אם תכתבו לנו דרך טופס "צרו קשר" ותצרפו את פרטיו (בעיקר כתובת אי-מייל) כדי שנוכל ליצור איתו קשר ולהציע לו פרסום בחינם בתור מורה לגיטרה בעמוד זה.
       אז תעשו קצת רעש של גיטרות <?php echo get_cat_name( $cat ); ?> ושיהיה בהצלחה!
       </div>
   
       <div id="googlead1">
       <a href="http://www.guitara.co.il/%D7%94%D7%A6%D7%A2%D7%94-%D7%9C%D7%97%D7%A0%D7%95%D7%99%D7%95%D7%AA-%D7%A0%D7%92%D7%99%D7%A0%D7%94/"><img src="wp-content/themes/mguitara/images/Square.jpg" alt="פרטים למפרסמים"/></a>
       </div>
   
       <div id="googlead2">
       <?php layerslider(3); ?>
       </div>
   
       	<?php else : ?>
   
       		<h2>לא נמצאו מאמרים</h2>
       		<?php include (TEMPLATEPATH .'/searchform.php'); ?>
   
       	<?php endif; ?>
   
       <?php get_footer(); ?><!-- template shel category מורים לגיטרה + children's -->
       ```
   
 * Any ideas?
 * Thanks.
 * [http://wordpress.org/plugins/wp-page-numbers/](http://wordpress.org/plugins/wp-page-numbers/)

The topic ‘Two Loops Problem’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/wp-page-numbers.svg)
 * [WP Page Numbers](https://wordpress.org/plugins/wp-page-numbers/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-page-numbers/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-page-numbers/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-page-numbers/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-page-numbers/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-page-numbers/reviews/)

## Tags

 * [multiple loops](https://wordpress.org/support/topic-tag/multiple-loops/)
 * [pages](https://wordpress.org/support/topic-tag/pages/)

 * 0 replies
 * 1 participant
 * Last reply from: [Begin](https://wordpress.org/support/users/bentalgad/)
 * Last activity: [12 years, 5 months ago](https://wordpress.org/support/topic/two-loops-problem/)
 * Status: not resolved