I've been working on this for the last 5 hours tearing my hair out. I'm trying to use multiple loops to show my custom posts from a specific taxonomy and have the code below running in a page template, but the title, excerpt and permalink are pointing to the page itself, not to the individual custom posts in my loops.
What's confusing is that:
- when I use
print_r($posts)
within my function to see whether the correct posts are being returned fromget_posts($args)
, it shows the correct info i.e. the title of the post and content etc. - The correct number of posts is being shown.
- The
if(have_posts($posts))
isn't displaying anything when there are no posts.
It's as if the ID of the page template is overriding my custom loops - can anyone see what the problem is before I go completely loopy (no pun intended)?
Thanks
Osu
/* ****************************************************************** */
/* !BOX LAYOUT */
/* ****************************************************************** */
// Function to create boxes
function osu_fbox($day, $cpt, $tax, $term) {
// Filter posts
$args = array(
'post_type' => $cpt,
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'slug',
'terms' => $term
)
)
);
$posts = get_posts( $args );
print_r($posts);
// Display content for this box
echo '<p class="fheading">' . $day .'</p>';
if (have_posts($posts)) :
foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<div id="lk-<?php echo sanitize_title($day); ?>-post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'literarykitchen' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark" class="readmore"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'literarykitchen' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark" class="readmore">Read more...</a>
</div>
<?php endforeach; ?>
<?php else: ?>
<p>No festival items currently scheduled</p>
<?php endif; wp_reset_query(); // Reset query so we can go again
}
// ------------------------------------------------------------------
// // !BOXES
// ------------------------------------------------------------------
// CPT and taxonomy
$cpt = 'lkfestival';
$tax = 'lkdays';
// Start if password protected
if ( post_password_required() ) : ?>
<p>We are currently working on the festivals area.</p>
<p>Please check back soon</p>
<?php echo get_the_password_form(); ?>
<?php else: ?>
<div id="weekdays" class="fcol">
<?php echo osu_fbox( 'Monday', $cpt, $tax, 'monday' ); ?>
<?php echo osu_fbox( 'Tuesday', $cpt, $tax, 'tuesday' ); ?>
<?php echo osu_fbox( 'Wednesday', $cpt, $tax, 'wednesday' ); ?>
<?php echo osu_fbox( 'Thursday', $cpt, $tax, 'thursday' ); ?>
<?php echo osu_fbox( 'Friday', $cpt, $tax, 'friday' ); ?>
</div> <!-- End #weekdays -->
<div id="saturday" class="fcol">
<?php echo osu_fbox('Saturday', $cpt, $tax, 'saturday' ); ?>
</div> <!-- End #saturday -->
<div id="sunday" class="fcol">
<?php echo osu_fbox('Sunday', $cpt, $tax, 'sunday' ); ?>
</div> <!-- End #sunday -->
<?php endif; /* End if post password protected */ ?>