Support

Account

Home Forums Front-end Issues 2 loops in a DIVI template page

Solved

2 loops in a DIVI template page

  • Hello,

    I try to display two loops from a custom post type in a template single page.

    The first one is diplayed but not the second.

    Thank for your help.

    Here my code :

    <?php
    
    /*
    Template Name: Comptes rendus
    */
    
    get_header();
    
    $show_default_title = get_post_meta( get_the_ID(), '_et_pb_show_title', true );
    
    $is_page_builder_used = et_pb_is_pagebuilder_used( get_the_ID() );
    
    ?>
    
    <div id="main-content">
    	<?php
    		if ( et_builder_is_product_tour_enabled() ):
    			// load fullwidth page in Product Tour mode
    			while ( have_posts() ): the_post(); ?>
    
    				<article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post' ); ?>>
    					<div class="entry-content">
    					<?php
    						the_content();
    					?>
    					</div> <!-- .entry-content -->
    
    				</article> <!-- .et_pb_post -->
    
    		<?php endwhile;
    		else:
    	?>
    	<div class="container">
    		<div id="content-area" class="clearfix">
    			<div id="left-area">
    			<?php while ( have_posts() ) : the_post(); ?>
    				<?php
    				/**
    				 * Fires before the title and post meta on single posts.
    				 *
    				 * @since 3.18.8
    				 */
    				do_action( 'et_before_post' );
    				?>
    				<article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post' ); ?>>
    					<?php if ( ( 'off' !== $show_default_title && $is_page_builder_used ) || ! $is_page_builder_used ) { ?>
    						<div class="et_post_meta_wrapper">
    							<h1 class="entry-title"><?php the_title(); ?></h1>
    
    						
    					<?php
    
    $query2021 = new WP_Query( array( 
    'post_type' => 'comptes-rendus',
    'orderby' => 'date',
    'order'   => 'DESC',
    
    'meta_query' => array(
    								array( 
    									'key' => 'annee', // name of custom field
    									'value' => '2021', // matches exaclty "123", not just 123. This prevents a match for "1234"
    									'compare' => 'LIKE' 
    								)
    )
    )
    );
    // The Query
    if ( $query2021->have_posts() ) : ?>
    <div class="entry-content" itemprop="text">
    <table class="table-cr">
      <thead>
        <tr>
          <th scope="col">Intitulé</th>
          <th scope="col">Date</th>
          <th scope="col">Fichier</th>
          <th scope="col">Poids</th>
        </tr>
      </thead>
    <?php // The Loop
    $file = get_field('fichier');
    while ( $query2021->have_posts() ) : $query2021->the_post(); ?>
      <tbody>
        <tr>
          <td data-label="Intitulé"><?php the_field('intitule'); ?></td>
          <td data-label="Date"><?php the_time( 'j.m.Y' ); ?></td>
          <td data-label="Fichier"><a href="<?php the_field('fichier'); ?>" target="_blank" ><button>Télécharger</button></a></td>
    	  <td data-label="Poids"><?php the_field('poids'); ?></td>
        </tr>
    <?php endwhile; wp_reset_query();  ?>
    
    </tbody>
    </table>
    </div>
    <?php
    $query2020 = new WP_Query( array( 
    'post_type' => 'comptes-rendus',
    'orderby' => 'date',
    'order'   => 'DESC',
    
    'meta_query' => array(
    								array( 
    									'key' => 'annee', // name of custom field
    									'value' => '2020', // matches exaclty "123", not just 123. This prevents a match for "1234"
    									'compare' => 'LIKE' 
    								)
    )
    )
    );
    // The Query
    elseif ( $query2020->have_posts() ) : ?>
    <div class="entry-content" itemprop="text">
    <table class="table-cr">
      <thead>
        <tr>
          <th scope="col">Intitulé</th>
          <th scope="col">Date</th>
          <th scope="col">Fichier</th>
          <th scope="col">Poids</th>
        </tr>
      </thead>
    <?php // The Loop
    $file = get_field('fichier');
    while ( $query2020->have_posts() ) : $query2020->the_post(); ?>
      <tbody>
        <tr>
          <td data-label="Intitulé"><?php the_field('intitule'); ?></td>
          <td data-label="Date"><?php the_time( 'j.m.Y' ); ?></td>
          <td data-label="Fichier"><a href="<?php the_field('fichier'); ?>" target="_blank" ><button>Télécharger</button></a></td>
    	  <td data-label="Poids"><?php the_field('poids'); ?></td>
        </tr>
    <?php endwhile; wp_reset_query();  ?>
    
    </tbody>
    </table>
    </div>
    
    						<?php	endif;
    						?>
    					</div> <!-- .et_post_meta_wrapper -->
    				<?php  } ?>
    
    					<div class="entry-content">
    					<?php
    						do_action( 'et_before_content' );
    
    						the_content();
    
    						wp_link_pages( array( 'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'Divi' ), 'after' => '</div>' ) );
    					?>
    					</div> <!-- .entry-content -->
    					<div class="et_post_meta_wrapper">
    					<?php
    					if ( et_get_option('divi_468_enable') === 'on' ){
    						echo '<div class="et-single-post-ad">';
    						if ( et_get_option('divi_468_adsense') !== '' ) echo et_core_intentionally_unescaped( et_core_fix_unclosed_html_tags( et_get_option('divi_468_adsense') ), 'html' );
    						else { ?>
    							<a href="<?php echo esc_url(et_get_option('divi_468_url')); ?>"><img src="<?php echo esc_attr(et_get_option('divi_468_image')); ?>" alt="468" class="foursixeight" /></a>
    				<?php 	}
    						echo '</div> <!-- .et-single-post-ad -->';
    					}
    
    					/**
    					 * Fires after the post content on single posts.
    					 *
    					 * @since 3.18.8
    					 */
    					do_action( 'et_after_post' );
    
    						if ( ( comments_open() || get_comments_number() ) && 'on' === et_get_option( 'divi_show_postcomments', 'on' ) ) {
    							comments_template( '', true );
    						}
    					?>
    					</div> <!-- .et_post_meta_wrapper -->
    				</article> <!-- .et_pb_post -->
    
    			<?php endwhile; ?>
    			</div> <!-- #left-area -->
    
    			<?php get_sidebar(); ?>
    		</div> <!-- #content-area -->
    	</div> <!-- .container -->
    	<?php endif; ?>
    </div> <!-- #main-content -->
    
    <?php
    
    get_footer();
  • Ok, i resolve myself my problem.
    My new code is :

    <?php
    
    /*
    Template Name: Comptes rendus
    */
    
    get_header();
    
    $show_default_title = get_post_meta( get_the_ID(), '_et_pb_show_title', true );
    
    $is_page_builder_used = et_pb_is_pagebuilder_used( get_the_ID() );
    
    ?>
    
    <div id="main-content">
    	<?php
    		if ( et_builder_is_product_tour_enabled() ):
    			// load fullwidth page in Product Tour mode
    			while ( have_posts() ): the_post(); ?>
    
    				<article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post' ); ?>>
    					<div class="entry-content">
    					<?php
    						the_content();
    					?>
    					</div> <!-- .entry-content -->
    
    				</article> <!-- .et_pb_post -->
    
    		<?php endwhile;
    		else:
    	?>
    	<div class="container">
    		<div id="content-area" class="clearfix">
    			<div id="left-area">
    			<?php while ( have_posts() ) : the_post(); ?>
    				<?php
    				/**
    				 * Fires before the title and post meta on single posts.
    				 *
    				 * @since 3.18.8
    				 */
    				do_action( 'et_before_post' );
    				?>
    				<article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post' ); ?>>
    					<?php if ( ( 'off' !== $show_default_title && $is_page_builder_used ) || ! $is_page_builder_used ) { ?>
    						<div class="et_post_meta_wrapper">
    							<h1 class="entry-title"><?php the_title(); ?></h1>
    						</div> <!-- .et_post_meta_wrapper -->
    				<?php  } ?>
    
    					<div class="entry-content">
    					<?php
    						do_action( 'et_before_content' );
    			
    
    $query2021 = new WP_Query( array( 
    'post_type' => 'comptes-rendus',
    'orderby' => 'date',
    'order'   => 'DESC',
    'meta_query' => array(
    						array( 
    						'key' => 'annee', // name of custom field
    						'value' => '2021', // matches exaclty "123", not just 123. This prevents a match for "1234"
    						'compare' => 'LIKE' 
    					)
    )
    )
    );
    						// The Query
    if ( $query2021->have_posts() ) : ?>
    <div class="entry-content" itemprop="text">
    <table class="table-cr">
      <thead>
        <tr>
          <th scope="col">Intitulé</th>
          <th scope="col">Date</th>
          <th scope="col">Fichier</th>
          <th scope="col">Poids</th>
        </tr>
      </thead>
    <?php // The Loop
    $file = get_field('fichier');
    while ( $query2021->have_posts() ) : $query2021->the_post(); ?>
      <tbody>
        <tr>
          <td data-label="Intitulé"><?php the_field('intitule'); ?></td>
          <td data-label="Date"><?php the_time( 'j.m.Y' ); ?></td>
          <td data-label="Fichier"><a href="<?php the_field('fichier'); ?>" target="_blank" ><button>Télécharger</button></a></td>
    	  <td data-label="Poids"><?php the_field('poids'); ?></td>
        </tr>
    
    </tbody>
    <? endwhile; ?>
    </table>
    </div>
    <?php endif ;
    
    $query2020 = new WP_Query( array( 
    'post_type' => 'comptes-rendus',
    'orderby' => 'date',
    'order'   => 'DESC',
    'meta_query' => array(
    						array( 
    						'key' => 'annee', // name of custom field
    						'value' => '2020', // matches exaclty "123", not just 123. This prevents a match for "1234"
    						'compare' => 'LIKE' 
    					)
    )
    )
    );
    						// The Query
    if ( $query2020->have_posts() ) : ?>
    <div class="entry-content" itemprop="text">
    <table class="table-cr">
      <thead>
        <tr>
          <th scope="col">Intitulé</th>
          <th scope="col">Date</th>
          <th scope="col">Fichier</th>
          <th scope="col">Poids</th>
        </tr>
      </thead>
    <?php // The Loop
    $file = get_field('fichier');
    while ( $query2020->have_posts() ) : $query2020->the_post(); ?>
      <tbody>
        <tr>
          <td data-label="Intitulé"><?php the_field('intitule'); ?></td>
          <td data-label="Date"><?php the_time( 'j.m.Y' ); ?></td>
          <td data-label="Fichier"><a href="<?php the_field('fichier'); ?>" target="_blank" ><button>Télécharger</button></a></td>
    	  <td data-label="Poids"><?php the_field('poids'); ?></td>
        </tr>
    
    </tbody>
    <? endwhile; ?>
    </table>
    </div>
    <?php endif; wp_reset_postdata(); ?>
    					
    					
    					</div> <!-- .entry-content -->
    					<div class="et_post_meta_wrapper">
    					<?php
    					
    					/**
    					 * Fires after the post content on single posts.
    					 *
    					 * @since 3.18.8
    					 */
    					do_action( 'et_after_post' );
    
    						
    					?>
    					</div> <!-- .et_post_meta_wrapper -->
    				</article> <!-- .et_pb_post -->
    
    			<?php endwhile; wp_reset_postdata(); ?>
    			
    			</div> <!-- #left-area -->
    
    			<?php get_sidebar(); ?>
    		</div> <!-- #content-area -->
    	</div> <!-- .container -->
    	<?php endif; ?>
    </div> <!-- #main-content -->
    
    <?php
    
    get_footer();

    Thank you !

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.