WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Loop within loop (3 posts)

  1. Rakesh Raja
    Member
    Posted 2 years ago #

    Ok

    I am using this in a custom page template called "Episodes"

    Ok here are requirements

    First one is custom post type "Performance" I call posts via

    query_posts( 'post_type=performance&meta_key=episode&meta_value=1' );

    Next one is episode

    I call posts via

    query_posts( array( 'post_type' => 'episode') );

    Episodes will have full episode videos while performance post type will have individual videos from that particular episode

    so to chain them together i have used a custom field called "episode"

    so what i want now is

    main loop episode and within that another loop for performance

    what i did is

    <?php
      query_posts( array( 'post_type' => 'episode') );
      if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>
    
     <?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE); ?>
    <h1>Episode :<?php echo get_post_meta($post->ID, 'episode', true) ?></h1>
    <h2><?php echo get_post_meta($post->ID, 'season', true) ?></h2><Br /><br />
    <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>
    
      query_posts( 'post_type=performance&meta_key=episode&meta_value=1' );
      if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>
    
    <li><?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?></li>
    
    <?php endwhile; endif; 
    
    <?php endwhile; endif;  ?>

    But it gave me nested loops.. it just keep loading..

    here is my desired layout

    [1] Episode 1 - [custom field assigned "episode" as value of "1"]
    [2] 4 Performances - [custom field assigned "episode" as value of "1"]

    so in output on page should be like this

    [ EPISODE 1 ]

    [PERFORMANCE 1] [PERFORMANCE 2] [PERFORMANCE 3] [PERFORMANCE 4]

    Help

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    multiple loops http://codex.wordpress.org/The_Loop

    try and use WP_Query() for the inner loop http://codex.wordpress.org/Class_Reference/WP_Query

  3. Rakesh Raja
    Member
    Posted 2 years ago #

    Thanks Alchymyth... it worked

    here is final code for other people

    <?php
      query_posts( array( 'post_type' => 'episode') );
      if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>
    
     <?php $my_meta = get_post_meta($post->ID,'_my_meta',TRUE); ?>
    <h1>Episode :<?php echo get_post_meta($post->ID, 'episode', true) ?></h1>
    <h2><?php echo get_post_meta($post->ID, 'season', true) ?></h2><Br /><br />
    <?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>
    
    <?php endwhile; endif;  ?>
    
    <?php
      $query = new WP_Query( array ( 'post_type' => 'performance', 'meta_key' => 'episode', 'meta_value' => '1' ) );
    while ( $query->have_posts() ) : $query->the_post();
    ?>
    
    <li><?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?></li>
    
    <?php endwhile;   ?>

Topic Closed

This topic has been closed to new replies.

About this Topic