title: code arrangement inside wordpress loop
body: <p>This is the actual code for the loop:</p>
  
  <pre><code>&lt;div id="content" class="span9" role="main"&gt;
  
  
  
          &lt;?php if ( have_posts() ){
  
          // Do we have any posts in the databse that match our query?
  
          ?&gt;
  
  
      &lt;?php 
  
      get_template_part( 'inc/post-format/single', get_post_format() ); 
  
      $enable_rtl = of_get_option('enable_rtl', false);
  
  
  
      if(!of_get_option('disable_pagination')){
  
          if($enable_rtl){
  
              $next_post = get_adjacent_post( false, '', true ); 
  
              $prev_post = get_adjacent_post( false, '', false ); 
  
              }else{
  
              $next_post = get_adjacent_post( false, '', false ); 
  
              $prev_post = get_adjacent_post( false, '', true ); 
  
              }
  
              ?&gt;
  
              &lt;?php
  
              }
  
  
  
          // show related posts by tag
  
          if(!of_get_option('disable_related_posts')){ 
  
          get_template_part( 'inc/related-posts' );
  
            }
  
          endwhile; // OK, let's stop the post loop once we've displayed it 
  
  
  
      // If comments are open or we have at least one comment, load up the default comment template provided by Wordpress
  
          if ( comments_open()  )
  
          comments_template( '', true );
  
  
  
           }else{ // Well, if there are no posts to display and loop through, let's apologize to the reader (also your 404 error) ?&gt;
  
  
  
              &lt;article class="post error"&gt;
  
                  &lt;h1 class="404"&gt;&lt;?php _e('Page not found', 'outbox'); ?&gt;&lt;/h1&gt;
  
              &lt;/article&gt;
  
  
  
          &lt;?php } // OK, I think that takes care of both scenarios (having a post or not having a post to show) ?&gt;
  
  
  
      &lt;/div&gt;&lt;!-- #content .site-content --&gt;
  </code></pre>
  
  <p>This is the snippet which I want to add: </p>
  
  <pre><code>// Parent CPT
  if ( 'cpt-parent' == wpse121567_get_cpt_hierarchy() ) {
      // Normal loop here
      if ( have_posts() ) : while ( have_posts() ) : the_post();
          // Normal loop markup
      endwhile; endif;
  } 
  // Child CPT
  else if ( 'cpt-child' == wpse121567_get_cpt_hierarchy() ) {
      // Globalize post object
      global $post;
  
      // Output Parent CPT title and content
      $parent = get_post( $post-&gt;post_parent );
      echo '&lt;h1&gt;' . $parent-&gt;post_title . '&lt;/h1&gt;';
      echo '&lt;div&gt;' . apply_filters( 'the_content', $parent-&gt;post_content ) . '&lt;/div&gt;';
  
      // Fetch parent CPT comments
      $parent_cpt_comments = get_comments( array(
          'post_id' =&gt; $post-&gt;post_parent,
          'status' =&gt; 'approve'
      ) );
      // Loop through parent CPT comments
      foreach ( $parent_cpt_comments as $comment ) {
          // Output comment list markup here
      }
  }
  // Grandchild CPT
  else if ( 'cpt-grandchild' == wpse121567_get_cpt_hierarchy() ) {
      // Comment Stats code goes here
  }
  </code></pre>
  
  <p>This is how I added it inside my actual code: </p>
  
  <pre><code>&lt;div id="content" class="span9" role="main"&gt;
  
  
  
  
      &lt;?php if ( 'cpt-parent' == wpse121567_get_cpt_hierarchy() ) {
          // Normal loop here
          if ( have_posts() ) : while ( have_posts() ) : the_post();
  
          get_template_part( 'inc/post-format/single', get_post_format() ); 
          endwhile; endif;
      } 
      // Child CPT
      else if ( 'cpt-child' == wpse121567_get_cpt_hierarchy() ) {
          // Globalize post object
          global $post;
  
          // Output Parent CPT title and content
          $parent = get_post( $post-&gt;post_parent );
          echo '&lt;h1&gt;' . $parent-&gt;post_title . '&lt;/h1&gt;';
          echo '&lt;div&gt;' . apply_filters( 'the_content', $parent-&gt;post_content ) . '&lt;/div&gt;';
  
          // Fetch parent CPT comments
          $parent_cpt_comments = get_comments( array(
              'post_id' =&gt; $post-&gt;post_parent,
              'status' =&gt; 'approve'
          ) );
          // Loop through parent CPT comments
          foreach ( $parent_cpt_comments as $comment ) {
              // Output comment list markup here
          }
      }
      // Grandchild CPT
      else if ( 'cpt-grandchild' == wpse121567_get_cpt_hierarchy() ) {
          // Comment Stats code goes here
      } 
  
      ?&gt;
  
              &lt;?php 
  
  
                  $enable_rtl         = of_get_option('enable_rtl', false);
  
  
  
                  if(!of_get_option('disable_pagination')){
  
                      if($enable_rtl){
  
                          $next_post = get_adjacent_post( false, '', true ); 
  
                          $prev_post = get_adjacent_post( false, '', false ); 
  
                      }else{
  
                          $next_post = get_adjacent_post( false, '', false ); 
  
                          $prev_post = get_adjacent_post( false, '', true ); 
  
                      }
  
                      ?&gt;
  
                      &lt;?php
  
                  }
  
  
  
              // show related posts by tag
  
              if(!of_get_option('disable_related_posts')){ 
  
                  get_template_part( 'inc/related-posts' );
  
              }
  
              endwhile; // OK, let's stop the post loop once we've displayed it 
  
  
  
              // If comments are open or we have at least one comment, load up the default comment template provided by Wordpress
  
              if ( comments_open()  )
  
                  comments_template( '', true );
  
  
  
           }else{ // Well, if there are no posts to display and loop through, let's apologize to the reader (also your 404 error) ?&gt;
  
  
  
          &lt;article class="post error"&gt;
  
              &lt;h1 class="404"&gt;&lt;?php _e('Page not found', 'outbox'); ?&gt;&lt;/h1&gt;
  
          &lt;/article&gt;
  
  
  
      &lt;?php } // OK, I think that takes care of both scenarios (having a post or not having a post to show) ?&gt;
  
  
  
  &lt;/div&gt;&lt;!-- #content .site-content --&gt;
  </code></pre>
  
  <p>And I'm getting this error: P**arse error: syntax error, unexpected T_ENDWHILE** in this line: <code>endwhile;</code> </p>
  
  <p>I'm sure is something wrong on how I added the code but it's very confusing for me because the actual code has a lot of php tags around...</p>
  
  <p>Any suggestion? Thanks!</p>