1

Why does my $featured_query treat every post as the first? Following Pieter's advice here, and similar posts, using the built-in loop counter I've been unable to display the excerpt on first post only.

front-page.php has a featured projects section, calling a template part:

<section id="featured">

    <?php // Start custom loop
        $args = array( 
                'post_type' => 'project',
                'posts_per_page' => 5,
                );
        $featured_query = new WP_Query( $args ); 

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

            get_template_part( 'template-parts/content-archive', 'project' );

        endwhile; 
        wp_reset_postdata(); // Reset loop data
    ?>

</section>

content-archive-project.php looks like this:

<article class="project">

<?php if ( has_post_thumbnail() ) { 
    the_post_thumbnail( 'post-thumbnail', 
        array( 'class' => 'featured' )); 
        } 
?>

    <header class="entry-header">

        <?php the_title( '<h2><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' ); ?>
        <p><?php
        $tagline  = get_post_meta( get_the_ID(), 'aj_post_tagline', true );
        echo esc_html( $tagline );
        ?></p>

    </header>

<?php if ( $featured_query->current_post == 0 ) { ?>

    <section class="entry-content">
           <?php the_excerpt(); ?>
    </section>

<?php } ?>

</article>
  • I only see one loop. Regardless, a "break" statement will usually end the loop. – RT Cunningham May 5 '17 at 12:44
  • I'm afraid I don't understand your answer, RT… are you able to suggest a solution? The code/loop above successfully displays the five most recent featured projects, on my homepage, each with their own unique excerpt. – the-first-man May 5 '17 at 13:18
  • I'm not sure that the post count comes through onto template parts. Can you try echoing the post count and seeing if it is actually providing a count? – rudtek May 5 '17 at 16:10
  • @rudtek, you're right, echoing the post count displays "1" next to each. Strange, they each have their own excerpts though... is there any way to resolve my original issue? Displaying first excerpt only? Seems very inefficient to have to duplicate code from a working file (content-archive-project.php) to do this. Defeats the point of template parts! – the-first-man May 5 '17 at 17:34
0

Pull it out of the content-part. That would be the easiest.

<section id="featured">

    <?php // Start custom loop
        $args = array( 
                'post_type' => 'project',
                'posts_per_page' => 5,
                );
        $featured_query = new WP_Query( $args ); 

        while ( $featured_query->have_posts() ) : $featured_query->the_post(); 
    ?>
    <article class="project">

    <?php 
        if ( has_post_thumbnail() ) { 
            the_post_thumbnail( 'post-thumbnail', 
            array( 'class' => 'featured' )); 
        } 
    ?>

    <header class="entry-header">

        <?php the_title( '<h2><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' ); ?>
        <p><?php
        $tagline  = get_post_meta( get_the_ID(), 'aj_post_tagline', true );
        echo esc_html( $tagline );
        ?></p>

    </header>

    <?php if ( $featured_query->current_post == 0 ) { ?>

        <section class="entry-content">
           <?php the_excerpt(); ?>
        </section>

    <?php } ?>

    </article>

    <php
        endwhile; 
        wp_reset_postdata(); // Reset loop data
    ?>

</section>

Another option would be to try this line:

include( locate_template( 'template-parts/content-archive-project.php', false, false ) ); 

instead of :

get_template_part( 'template-parts/content-archive', 'project' );

but you should test that as I didn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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