WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] "The Loop" running "else" statement even though posts exist (3 posts)

  1. CoBu1
    Member
    Posted 9 months ago #

    I am trying to create a loop for a custom post type called "portfolio". I have published portfolio posts but for some reason my loop is running the "else" statement and echoing "WHERE MY POSTS?". Can anyone tell me why?

    <?php
    		$args = array(
    			'post_type' => 'portfolio',
    			'tax_query' => array(
    				array(
    					'taxonomy' => 'portfolio_category',
    					'field' => 'slug',
    					'terms' => 'boardgames'
    				)
    			)
    		);
    		$portfolio = new WP_Query( $args );
    		if( $portfolio->have_posts() ) {
    			while( $portfolio->have_posts() ) {
    				$portfolio->the_post();
    				?>
    					<h1><?php the_title(); ?></h1>
    					<div class='content'>
    						<?php the_content(); ?>
    					</div>
    
    					<div class="four columns">
    				<a href="<?php the_permalink ?>">
    					<div class="img-wrp">
    				        <img class="scaleimg" src="<?php the_post_thumbnail ?>" />
    				        <div class="overlay-wrp">
    		    		        <i class="icon loupe overlay-content"></i>
    		    		        <div class="overlay"></div>
    				        </div>
    					</div>
    			    </a>
    				<p class="project-title"><?php the_title(); ?></p>
    				<p class="project-description"><?php the_excerpt(); ?></p>
    			</div><?php
    			}
    		}
    		else {
    			echo 'WHERE MY POSTS?';
    		}
    
    		?>
  2. essaysnark
    Member
    Posted 9 months ago #

    Totally not a PHP expert but... The "if" and the "while" seem redundant to me (and the opposite order would be problematic: http://codex.wordpress.org/Function_Reference/have_posts#Note )

    Here's a basic version of what you're trying to do (from http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress ) - maybe you can adapt?

    <?php $loop = new WP_Query( array( 'post_type' => 'super_duper', 'posts_per_page' => 10 ) ); ?>
    
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    
    	<?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>
    
    	<div class="entry-content">
    		<?php the_content(); ?>
    	</div>
    <?php endwhile; ?>
  3. CoBu1
    Member
    Posted 9 months ago #

    That fixed it! Thank you so much.

Reply

You must log in to post.

About this Topic