0

I have a CPT for "Band Members", and I want to list all of them at the top of a template, above all the posts (which will just be featured images) - a sort-of Navigation for all members. I can't think of how to do this... is there two loops? Any help would be appreciated!

<div id="main">
<?php


$args=array(
  'post_type' => 'band_member',
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1 // WHAT IS THIS??
  );

$my_query = null;
$my_query = new WP_Query($args);
echo '<div class="post-nav">'; 
if( $my_query->have_posts() ) {

  while ($my_query->have_posts()) : $my_query->the_post();

  ?>


<a href="<?php the_permalink() ?>" rel="bookmark" ><?php the_title(); ?></a>


<?php
  endwhile;
}
echo '</div><!-- end post nav -->';

wp_reset_query();  // Restore global post data stomped by the_post().

?>

<?php
// PUT ALL CPT FEATURED IMAGES HERE...
?>
</div><!-- /main -->

END OF ORIGINAL CODE.....

NEW CODE WITH HELP.....

<section id="main"> 
<?php
$args=array(
  'post_type' => 'band_member',
  'post_status' => 'publish',
  'posts_per_page' => -1,
  );

$my_query = null;
$my_query = new WP_Query($args);
echo '<div class="post-nav">'; 
if( $my_query->have_posts() ) {

  while ($my_query->have_posts()) : $my_query->the_post();

  ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" ><?php the_title(); ?></a> 
    <?php
  endwhile;
}
echo '</div><!-- end post nav -->';

wp_reset_query();  // Restore global post data stomped by the_post().

?>
<?php

$args=array(
    'post_type' => 'band_member',
    'post_status' => 'publish',
    'posts_per_page' => -1,
  );

  $my_query = null;
  $my_query = new WP_Query($args);

       if( $my_query->have_posts() ) {

       while ($my_query->have_posts()) :  $my_query->the_post();
       ?>
    <a href="<?php the_permalink() ?>" rel="bookmark" ><?php the_post_thumbnail(); ?></a> 
<?php
     endwhile;
  }

  wp_reset_query();  // Restore global post data stomped by the_post().

?>
            </section><!-- /#main -->
3
  • I don't understand why you run the same loop twice, one to display the title and one to display thumbnails Jun 4, 2014 at 7:29
  • Do you want one loop with just images and one loop with just titles. Jun 4, 2014 at 7:30
  • Yes, I can see how that might be confusing. It is for a listing of many members of a group. What I'm aiming for is to have the titles as a "mini nav" preceding all of the thumbnails, also to be included on single post pages of this CPT.
    – alexwc_
    Jun 4, 2014 at 7:33

2 Answers 2

1

You are on the right track here, but it is a bit bumpy

Firstly, caller_get_posts is depreciated, that has been replaced with ignore_sticky_posts

Secondly, you should use wp_reset_postdata not wp_reset_query. The latter is used in conjuction with query_posts which should never be used.

Thirdly, your call to images should be inside your loop, not outside.

Ok, that cleared, you should run two loop here if you need other posts after the this first query. You should have a look WP_Query on running multiple loops. But that all depends on what you need to achieve. Just one note, the codex page on multiple loops uses query_posts which you must not use. Stick with WP_Query

To come back to your loop for above, it should look something like this

$args=array(
    'post_type' => 'band_member',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'ignore_sticky_posts'=> 1 
  );

  $my_query = null;
  $my_query = new WP_Query($args);

       if( $my_query->have_posts() ) {

       while ($my_query->have_posts()) :  $my_query->the_post();

      <-----get your images----->

 <?php
     endwhile;
  }

  wp_reset_query();  // Restore global post data stomped by the_post().

EDIT

As you are a newbie, here is a great tip. When you develop a theme/plugin, or simply just add code to your theme, always set debug to true in wp-config.php. This will immediatly print errors to your screen if there are any. Just remember, never leave debug on true on a live site. Set to false immediatly after your done.

For further reading: Debugging Wordpress

EDIT 2

Having a look at the code you posted now, you can do that in one one loop. You should use rewind_posts() here

<?php
$args=array(
  'post_type' => 'band_member',
  'post_status' => 'publish',
  'posts_per_page' => -1,
  );

$my_query = null;
$my_query = new WP_Query($args);
echo '<div class="post-nav">'; 
if( $my_query->have_posts() ) {

  while ($my_query->have_posts()) : $my_query->the_post();

  ?>
    <a href="<?php the_permalink() ?>"rel="bookmark" ><?php the_title(); ?></a> 
    <?php
  endwhile;
}
echo '</div><!-- end post nav -->';

 $my_query->rewind_posts();

       while ($my_query->have_posts()) :  $my_query->the_post();

  ?> 

    <a href="<?php the_permalink() ?>"rel="bookmark" ><?php the_post_thumbnail(); ?></a> 
   <?php
     endwhile;

?>
7
  • yes... this is almost exactly what I want. I can't add too much in the comments, so I'll edit my question with my final code. If you have a chance to see if it is ok, I'd appreciate it!
    – alexwc_
    Jun 4, 2014 at 7:21
  • yes! it works, though being "green" I don't know if it is clear and concise.
    – alexwc_
    Jun 4, 2014 at 7:26
  • I'll accept once we are done for sure!
    – alexwc_
    Jun 4, 2014 at 7:39
  • There was a small syntax error in the code in EDIT 2. I've corrected it. You should be good to go. BTW, your code doesn't look bad in your edit. But you can do that in one one loop. Didn't know that you needed to do that what you have done. Hope this help Jun 4, 2014 at 13:21
  • Also rewind_posts() should be $my_query->rewind_posts() Jun 4, 2014 at 13:25
0

to save you from having to use wp_reset_query ...

    $args = array(
            'post_type' => 'band_member',
            'post_status' => 'publish',
            'posts_per_page' => -1
    );
    $show_band_members = get_posts( $args );
    if ($show_band_members) {
     echo '<div class="post-nav">'; 

       foreach ($show_band_members as $post) {
         setup_postdata( $post );
         ?>
         <a href="<?php the_permalink() ?>" rel="bookmark" ><?php the_title(); ?></a>
         <?php
    }
}
echo "</div>";

An explanation of all of the parameters for get_posts can be found here:

http://codex.wordpress.org/Class_Reference/WP_Query

1
  • You should add the relevant explanation to your answer Jun 4, 2014 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.