title: Wrap post type post with nested child post inside ul
body: <p>I have some problem when trying to achieve this HTML structure in a wordpress loop.</p>
  
  <pre><code>&lt;ul class="dropdown"&gt;
      &lt;li&gt;Normal Text Without Child&lt;/li&gt;
      &lt;li&gt;Link Text Without Child&lt;/li&gt;
      &lt;li class="has-child"&gt;
              &lt;a href="#"&gt;Link Text With Child&lt;/a&gt;
              &lt;ul&gt;
                      &lt;li&gt;&lt;a href="#"&gt;Child Link Text&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;Child Link Text&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;Child Link Text&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;Child Normal Text&lt;/li&gt;
                      &lt;li&gt;Child Normal Text&lt;/li&gt;
                      &lt;li&gt;Child Normal Text&lt;/li&gt;
              &lt;/ul&gt;
      &lt;/li&gt;
       &lt;li class="has-child"&gt;
              Normal Text With Child
              &lt;ul&gt;
                      &lt;li&gt;&lt;a href="#"&gt;Child Link Text&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;Child Link Text&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;&lt;a href="#"&gt;Child Link Text&lt;/a&gt;&lt;/li&gt;
                      &lt;li&gt;Child Normal Text&lt;/li&gt;
                      &lt;li&gt;Child Normal Text&lt;/li&gt;
                      &lt;li&gt;Child Normal Text&lt;/li&gt;
              &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>The code is responsible to displaying my Custom Post Type Called <code>Product</code> which have <code>page</code> and <code>hierarchical</code> attributes so I can set post to have parent. I write this code in <code>taxonomy-product_category.php</code>. Here's my attempt:</p>
  
  <pre><code>&lt;?php
              global $post;
              $child = false;
              $type   = get_term_by('slug',get_query_var('term'),get_query_var('taxonomy'));
              $loop = new WP_Query(
              array(
                      'post_type' =&gt; 'product',
                      'orderby' =&gt; 'title',
                      'order' =&gt; 'ASC',
                      'posts_per_page' =&gt; -1,
                      'tax_query' =&gt; array(
                              array(
                                      'taxonomy'  =&gt; 'product_category',
                                      'field'     =&gt; 'slug',
                                      'terms'     =&gt; $type-&gt;slug
                              )
                      ),
              )
              );?&gt;
  
              &lt;?php if($loop-&gt;have_posts()){while($loop-&gt;have_posts()):$loop-&gt;the_post();
                      $post;
                      $detail = sunterCustomField('detail'); //metabox that will check if this post able to show link or not
                      $text = ($detail == 1) ? '&lt;a href="'.get_permalink().'"&gt;'.get_the_title().'&lt;/a&gt;' : get_the_title();
              ?&gt;
                &lt;li&gt;
                     &lt;?php echo $text;?&gt;
                      &lt;?php if($post-&gt;post_parent &lt;&gt; 0 &amp;&amp; $child == false):?&gt;
                      &lt;ul&gt;
                    &lt;li&gt;&lt;?php echo $text;?&gt;&lt;/li&gt;
                      &lt;?php
                      $child = true;
                      endif;?&gt;
                      &lt;?php if($post-&gt;post_parent == 0 &amp;&amp; $child == true):?&gt;
                      &lt;/ul&gt;
                      &lt;?php
                      $child = false;
                      endif;?&gt;
                              &lt;/li&gt;
              &lt;?php endwhile;} ?&gt;
              &lt;?php wp_reset_query(); ?&gt;
  </code></pre>
  
  <p>Note : I can't use <a href="http://codex.wordpress.org/Function_Reference/wp_list_pages" rel="nofollow">wp_list_pages()</a> because not every post would have a link and i need to querying only post type in same term ( that's why i write it in taxonomy-product_category.php ).Here the result of code above.</p>
  
  <pre><code>        &lt;ul class="dropdown"&gt;
        &lt;li&gt;
              &lt;a href="#"&gt;A product Chemical 1&lt;/a&gt;
              &lt;li&gt;
                      A product Chemical 6 child
              &lt;ul&gt;
                      &lt;li&gt;A product Chemical 6 child&lt;/li&gt;
                      &lt;li&gt;A product Chemical 7 child &lt;/li&gt;
                       &lt;li&gt;B product Chemical 2 &lt;/li&gt;
              &lt;/ul&gt;
              &lt;/li&gt;
               &lt;li&gt;&lt;a href="#"&gt;C product Chemical 3&lt;/a&gt;
      &lt;/ul&gt;
  </code></pre>
  
  <p>See, it doesn't appear properly. I can't figure out the logic.</p>
  
  <p>SOLUTION !!</p>
  
  <p>Well , after couple hour googling for this problem , finally i can solve it.So , the basic idea is conditional statement which detect if current post has children post and check if current post are top level post.Thanks for <a href="https://gist.github.com/lukaszklis/1247306" rel="nofollow">this</a> awesome snippet which i borrow and edit it a little bit so it can be suit with my problem.I am adding post type args to the function which is product post type</p>
  
  <pre><code>function has_children() {
  global $post;
  $args = array(
      'child_of'  =&gt; $post-&gt;ID,
      'post_type' =&gt; 'product'
  );
  $pages = get_pages($args);
  return count($pages);
  }
  
  function is_top_level() {
  global $post, $wpdb;
  $current_page = $wpdb-&gt;get_var("SELECT post_parent FROM $wpdb-&gt;posts WHERE ID = " . $post-&gt;ID);
  return $current_page;
  }
  </code></pre>
  
  <p>Yep , the code above added in functions.php.And here it's the code in taxonomy-product_catgeory.php</p>
  
  <pre><code>&lt;?php
      $loop = new WP_Query(
      array(
              'post_type' =&gt; 'product',
              'orderby' =&gt; 'title',
              'order' =&gt; 'ASC',
              'posts_per_page' =&gt; -1,
              'tax_query' =&gt; array(
                      array(
                              'taxonomy'  =&gt; 'product_category',
                              'field'     =&gt; 'slug',
                              'terms'     =&gt; $type-&gt;slug
                      )
              ),
      )
      );?&gt;
      &lt;?php if($loop-&gt;have_posts()){while($loop-&gt;have_posts()):$loop-&gt;the_post();
              $detail = sunterCustomField('detail');
              $text = ($detail == 1) ? '&lt;a href="'.get_permalink().'"&gt;'.get_the_title().'&lt;/a&gt;' : get_the_title();
      ?&gt;
      &lt;?php
  
      if (has_children()) {
              ?&gt;
              &lt;li class="has-child"&gt;
                      &lt;?php echo $text;?&gt;
                      &lt;ul&gt;
                              &lt;?php
                              $args = array(
                                      'child_of' =&gt; $post-&gt;ID,
                                      'parent' =&gt; $post-&gt;ID,
                                      'post_type'     =&gt; 'product',
                                       'hierarchical' =&gt; 0
                              );
                              $pages = get_pages($args);
                              foreach ($pages as $page) {
                                      $subdetail =  get_post_meta( $page-&gt;ID, 'detail', true );
                                      $subtitle = ($subdetail == 1) ? '&lt;a href="'.get_permalink($page-&gt;ID).'"&gt;'.$page-&gt;post_title.'&lt;/a&gt;' : $page-&gt;post_title;
                                      echo '&lt;li&gt;'.$subtitle.'&lt;/li&gt;';
                              }
                              ?&gt;
                      &lt;/ul&gt;
              &lt;/li&gt;
  
              &lt;?php
      }
      if(!is_top_level() &amp;&amp; !has_children()){
              ?&gt;
              &lt;li&gt;&lt;?php echo $text;?&gt;&lt;/li&gt;
              &lt;?php
      }
  
      ?&gt;
      &lt;?php endwhile;} ?&gt;
      &lt;?php wp_reset_query(); ?&gt;
  </code></pre>