Forums

[resolved] Always three posts from multiple custom post types (2 posts)

  1. thiesenmurray
    Member
    Posted 1 year ago #

    I'm having a problem with a custom query loop. I want on the homepage always a number of three posts. Some of that posts are events so sometimes there are three but there are also times that there are zero or two posts. I always want to fill in the left over spaces with other posts from other custom post types. The events are the most important so I want to say in my loop: count the number of events and if there're less then three, show another custom post type to fill up the three.

    Thanks in advance

  2. thiesenmurray
    Member
    Posted 1 year ago #

    Guys, I fixed it by myself. Here's the code with comments

    <!-- here starts the normal loop -->
    <?php query_posts( 'post_type=events'); ?>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    	<!-- There's a count in the loop that starts at zero. After 3 he break's the loop -->
    	<?php static $count = 0;
    	if ($count == "3") { break; }
    	else { ?>
    
    		<!-- The normal loop -->
    		<div class="test">
    			<h1><?php the_title(); ?></h1>
    			<?php the_content(); ?>
    		</div>
    
    	<!-- before running the loop again, php adds 1 up in the count variable -->
    	<?php $count++; } ?>
    	<?php endwhile; ?>
    
    	<!-- Setting a if statement. If the loop is 1, then show another loop with an custom post type -->
    	<?php if ( $count == "1" ) { ?>
    		<?php query_posts( 'post_type=media&showposts=2'); ?>
    			<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    				<div class="test">
    					<h1><?php the_title(); ?></h1>
    					<?php the_content(); ?>
    				</div>
    			<?php endwhile; ?>
    		<?php endif; ?>
    	 <?php } ?>
    
    	<!-- Setting a if statement. If the loop is 2, then show another loop with an custom post type -->
    	<?php if ( $count == "2" ) { ?>
    		<?php query_posts( 'post_type=media&showposts=1'); ?>
    			<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    			     <div class="test">
    			     	<h1><?php the_title(); ?></h1>
    			     	<?php the_content(); ?>
    			     </div>
    			<?php endwhile; ?>
    		<?php endif; ?>
    	<?php } ?>
    
    <?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic