Question

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:

  1. when I use print_r($posts) within my function to see whether the correct posts are being returned from get_posts($args), it shows the correct info i.e. the title of the post and content etc.
  2. The correct number of posts is being shown.
  3. 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 */ ?>
Was it helpful?

Solution 2

Ok, I figured out how to get it working, so I'll post the answer here. However, I thought I should be able to use the normal WP template tags like the_title(); and the_excerpt(); within a get_posts() loop? Am I doing this the right way or can I improve this answer?

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);
    // echo 'COUNT: ' . count($posts);

    // Display content for this box
    echo '<p class="fheading">' . $day .'</p>';
    if (count($posts) > 0) :
        foreach ( $posts as $post ) : setup_postdata( $post );
        $id         = $post->ID;
        $title      = $post->post_title;
        $excerpt    = $post->post_excerpt;
        $pl         = get_permalink( $post->ID );
        ?>

        <div id="lk-<?php echo sanitize_title($day); ?>-post-<?php echo $id; ?>">
            <h2><a href="<?php echo $pl; ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'literarykitchen' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark" class="readmore"><?php echo $title; ?></a></h2>
            <?php echo $excerpt; ?>
            <a href="<?php echo $pl; ?>" 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 */ ?>

OTHER TIPS

You should put wp_reset_query() somewhere before your calls to the custom post functions (the_title(), the_content(),...).

From the Wordpress codex:

wp_reset_query() restores the $wp_query and global post data to the original main query. This function should be called after query_posts(), if you must use that function. As noted in the examples below, it's heavily encouraged to use the pre_get_posts filter to alter query parameters before the query is made.

http://codex.wordpress.org/Function_Reference/wp_reset_query

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top