I’m having some issues adding multiple loops. I have a custom post type (locations) that uses a single page template (single-locations.php). I’m trying to add additional loops to this template that will display (x) number of posts from different categories (regular post categories) depending on what custom post type id a user is on.
The issue that is occurring is that, based on the code below, all the different custom post type id’s defined are loading the same 5 posts (shown in the first query) but not from the specific categories defined. Additionally the queries under each custom post type id’s are not being honored. I’m not sure if I’ve got the if/elseif statements messed up or if the main custom post type loop is interfering.
Any advice on how to get this working would be great. 🙂
<div id="content" class="col-full">
<section id="secondary-content" class="col-left">
<?php
global $post;
$meta_history = get_post_meta($post->ID, 'bb-history', true );
$meta_message = get_post_meta($post->ID, 'bb-mayors-message', true );
$meta_news = get_post_meta($post->ID, 'bb-current-news', true );
?>
<h1><?php the_title(); ?></h1>
<?php echo $meta_history; ?>
<h1>Mayor's Message</h1>
<?php echo $meta_message; ?>
<h1>Current News</h1>
<?php echo $meta_news; ?>
<?php
// get_post_type (44)
if ( 'locations' == get_post_type( 44 ) ){
// Display 5 Posts From Category 5
$args = array(
'posts_per_page' => 5,
'category' => '5',
'orderby' => 'post_date',
'order' => 'DESC');
$loc1 = new WP_Query($args);
while ( $loc1->have_posts() ) : $loc1->the_post(); global $post;
?>
<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<?php
the_excerpt();
endwhile; wp_reset_postdata();
// get_post_type (46)
} else if ( 'locations' == get_post_type( 46 ) ) {
// Display 3 Posts From Category 10
$args = array(
'posts_per_page' => 3,
'category' => '10',
'orderby' => 'post_date',
'order' => 'DESC');
$loc2 = new WP_Query($args);
while ( $loc2->have_posts() ) : $loc2->the_post(); global $post;
?>
<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<?php
the_excerpt();
endwhile; wp_reset_postdata();
// get_post_type (52)
} else if ( 'locations' == get_post_type( 52 ) ) {
// Display 3 Posts From Category 7
$args = array(
'posts_per_page' => 3,
'category' => '7',
'orderby' => 'post_date',
'order' => 'DESC');
$loc3 = new WP_Query($args);
while ( $loc3->have_posts() ) : $loc3->the_post(); global $post;
?>
<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<?php
the_excerpt();
endwhile; wp_reset_postdata();
// get_post_type (55)
} else if ( 'locations' == get_post_type( 55 ) ) {
// Display 3 Posts From Category
$args = array(
'posts_per_page' => 3,
'category' => '8',
'orderby' => 'post_date',
'order' => 'DESC');
$loc4 = new WP_Query($args);
while ( $loc4->have_posts() ) : $loc4->the_post(); global $post;
?>
<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<?php
the_excerpt();
endwhile; wp_reset_postdata();
}
// get_post_type (57)
else if ( 'locations' == get_post_type( 57 ) ) {
// Display 3 Posts From Category 9
$args = array(
'posts_per_page' => 3,
'category' => '9',
'orderby' => 'post_date',
'order' => 'DESC');
$loc5 = new WP_Query($args);
while ( $loc5->have_posts() ) : $loc5->the_post(); global $post;
?>
<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<?php
the_excerpt();
endwhile; wp_reset_postdata();
}
?>
</section>
<section id="primary-content" class="col-left">
<!-- Front Page Articles -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article <?php post_class(); ?> role="article">
<section class="entry fix">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:' ), 'after' => '</div>' ) ); ?>
</section>
</article><!-- /.post -->
<?php endwhile; //End WhILE Loop ?>
<?php else : ?>
<article <?php post_class(); ?>>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
</article><!-- .post -->
<?php endif; //End IF Statement ?>
</section>
<!-- End #main -->
<?php get_sidebar(); ?>
</div>
<!-- End #content -->
It sounds like what you are trying to do is make a kind-of “related posts” feature for your “Locations” single post display based on the category.
You don’t need to be checking to see if the post is in the “Locations” post type. You are using
single-locations.php
unless you site is broken only “Locations” posts should show up there. You really only need to check the ID.I took the liberty of cleaning up your code.
$args
array every time since most of it doesn’t change.category
tocat
That is a lot of code to read. Apologies if I missed something but I think that should work for you a little better.