Support » Fixing WordPress » custom post_type loop

  • Resolved pybiru

    (@pybiru)


    Hi,

    I have a custom “post_type” called cities and need to loop through all the cites, outputting name, thumbnail and excerpt. I have the following but its not working..

    <?php
    
    	$args = array('post_type' => 'cities', 'depth' => 1, 'orderby' => 'name', 'posts_per_page'  => 20, 'post_status' => 'publish');
    	$loop = new WP_Query($args);
    
    	wp_list_pages($args);
    
    	if($loop->have_posts()){
    
              while ($loop->have_posts()) : $loop->the_post(); ?>
                <li>
                  <a href='<?php the_permalink() ?>' rel='bookmark' title='<?php the_title_attribute(); ?>'>
                    <?php the_title(); ?>
                  </a>
                  <?php the_excerpt() ?>
                  <?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
                </li>
              <?php
              endwhile;
              } //if ($my_query)
        wp_reset_query();*/
    
    ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • Start by removing wp_list_pages. Then be explicit in ‘what’s not working’.

    Thanks for reply Peter. The “wp_list_pages” should have been commented out, I had the titles pulling in/ working using it. But I need thumbnails and some content/ excerpt too.

    Whats not working, I need the title, thumbnail and excerpt for each city to be output in the loop. At the moment nothing is getting output.

    Try this. Use this for the loop. If it works, add the code to get the thumbnail.

    // The Loop
    if($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post(); ?>
    <li>
        	<a href='<?php the_permalink() ?>' rel='bookmark' title='<?php the_title_attribute(); ?>'>
    	<?php the_excerpt(); ?>
    </li>
    <?php endwhile; else: ?>
    <p>Oops, there are no posts.</p>
    <?php wp_reset_postdata(); // reset the query ?>
    <?php endif; ?>

    Thanks for help Peter, I was getting syntax errors from your code(probably sloppy integration, not great in PHP).

    I have the following working, but its outputting all the child pages aswell. I just want all the top level pages. I added “depth” => 1, but doesn’t seem to work.

    <?php query_posts(array('post_type'=>'cities', 'depth' => 1)); ?>
    
        <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    
        <h3>
          <a href="<?php echo the_permalink(); ?>"><?php echo the_title(); ?></a>
          <?php the_content(); ?>
        </h3>
    
        <?php endwhile; ?>

    No, stick with wp_query. Just switch everything you had in your first post with this:

    <?php
    	$args = array('post_type' => 'cities', 'depth' => 1, 'orderby' => 'name', 'posts_per_page'  => 20, 'post_status' => 'publish');
    	$wp_query = new WP_Query($args);
    ?>
    <?php
    	// The Loop
    	if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post(); ?>
    	<li>
    		<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
    		<?php the_excerpt(); ?>
    	</li>
    	<?php endwhile; else: ?>
    	<p>Oops, there are no posts.</p>
    	<?php wp_reset_postdata(); // reset the query ?>
    	<?php endif; ?>

    Ah, great. That’s working beautifully now. One more thing, I have (‘depth’ => 1), in the $args array yet it is outputting the child pages too. Is there a differen’t way to only get the parent pages?

    Thanks a million for the help/advice.

    remove depth, use ‘post_parent’ => 0

    working with just parents pages now, the final code…

    <?php
          $args = array('post_type' => 'cities', 'depth' => 1, 'orderby' => 'name', 'post_status' => 'publish', 'post_parent' => 0, 'posts_per_page'=>'-1');
          $wp_query = new WP_Query($args);
        ?>
        <?php if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post(); ?>
    
            <li>
              <div class="city">
    
                  <?php the_post_thumbnail(); ?>
    
                  <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
                  <?php the_excerpt(); ?>
    
              </div>
            </li>
    
          <?php endwhile; else: ?>
            <p>Oops, something has gone wrong. No cities currently available.</p>
            <?php wp_reset_postdata(); // reset the query ?>
          <?php endif; ?>
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘custom post_type loop’ is closed to new replies.