Start loop and continue it in included template? Possible?

Hi,

Why isn’t this possible and is there any way to get it to work? The loop continues in the template part (while part etc).

		<?php
			$args = array(
				'post_type' => 'case'
			); 
			$the_query = new WP_Query( $args );
		?>
		<?php if ( $the_query->have_posts() ) : ?>
		
			<?php get_template_part('templates/widget', 'slider'); ?>

		<?php endif; wp_reset_query(); ?>

Are you using $the_query object in the template part too, or do you have something else? Could you please post the rest of the query from the template part?

sure, here is the full template part:

<section id="patientcase" class="slider-widget widget widget--slider gray-bg">
	<div class="row">
		<div class="small-12 columns">
			<div class="widget__inner">
				<div class="swiper-container swiper-container--widget">
				    <div class="swiper-wrapper">

				    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

				    	<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' ); ?>

						<article class="slide swiper-slide">
							<div class="slide__inner">
								<div class="slide__thumb">
									<figure
										style="background-image: url('<?php echo $thumb[0]; ?>');">
									</figure>
								</div>
								<div class="slide__content">
									<h1 class="section-heading slide__title">
										<?php the_title(); ?>
									</h1>
									<div class="slide__ingress">
										<?php the_excerpt(); ?>
									</div>
									<a href="<?php the_permalink(); ?>" class="btn btn--regular">Patientcase</a>
									<a href="#" class="btn btn--outline">Se filmen</a>
								</div>
							</div>						
						</article>

				    <?php endwhile; ?>

				    </div>
				    <div class="swiper-button-prev swiper-btn swiper-btn--widget"></div>
					<div class="swiper-button-next swiper-btn swiper-btn--widget"></div>
					<div class="swiper-pagination swiper-pagination--widget"></div>
				</div>				
			</div>
		</div>
	</div>
</section>

At the beginning of this code, just before you set up $args, try adding global $post .

Hmm doing so killed the entire page, any idea why?

So your code was like this? Then I have no idea why it messed up your page. I’ve never had problems with this.

<?php	
global $post;
$args = array(				
     'post_type' =&gt; 'case'			
); 			
$the_query = new WP_Query( $args );		
?>

Is this the only loop you are using on this page?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.