0

I am trying to display a single "featured post" in the archive.php template using a custom query. This featured post should be the most recent post in that category or tag. I'm using pagination for the rest of the archive page (also a custom query so that pagination is retained), and this same post should appear at the top of all the subsequent pages (i.e. /page/2/, /page/3/, etc.)

I've got this working on home.php, but archive.php is showing the most recent post from each page, not overall.

This is what I'm using in archive.php.

global $query_string;
$query_args = explode("&", $query_string);

$args = array(
    'posts_per_page' => 1,
    'post_not_in' => get_option( 'sticky_posts' )
);

$query1 = new WP_Query($args);

if ( $query1->have_posts() ) {  
    while( $query1->have_posts() ) {  
        $query1->the_post();      
        get_template_part( 'content', 'latest' );
    }
} 

wp_reset_postdata(); 

And here's the working code from home.php:

    $args = array(
        'posts_per_page' => 1,
        'post_not_in' => get_option( 'sticky_posts' )
    );

    $query1 = new WP_Query($args);

    if ( $query1->have_posts() ) {  
        while( $query1->have_posts() ) {  
            $query1->the_post();      
            get_template_part( 'content', 'latest' );
        }
    } 

    wp_reset_postdata();

Hope I've expressed myself clearly. Any help would be greatly appreciated :)

  • Archive.php pulls posts by post date, not by category. So, you will have posts from different categories on the same page. Why aren't you using category.php for this? – unifiedac May 23 '14 at 17:34
  • @unifiedac your sentence is not true. Please, see the template hierarchy in the codex. – cybmeta May 23 '14 at 17:41
  • Good call. I don't know why I forgot that. – unifiedac May 23 '14 at 17:55
1

I'm assuming that you have a loop to display the top (static) post and a second loop for the paginated posts:

[header]

[static latest post] loop 1

[paginated content] loop 2

[footer]

For loop 1 you may need to clearly identify the category in your custom loop.

$my_cat = get_query_var('cat');
$args = array(
    'posts_per_page' => 1,
    'cat'            => $my_cat,
    'orderby'        => 'date',
    'order'          => 'DESC',
    'post_not_in'    => get_option( 'sticky_posts' )
);
  • Yes, I'm using get_template_part to pull the first loop into the archive template, which has the main paginated loop. Your answer is also along the right lines, in that I need to tell the first custom query to get posts in the current category, but I accepted @cybnet's answer because he was first :) – markcbain May 24 '14 at 6:08
  • Technically my answer was first (by two minutes), but I'm glad you got what you needed! – unifiedac May 27 '14 at 22:28
  • You're right, sorry. I didn't see the datestamp the other day and presumed the first answer on the list was the first chronologically. – markcbain May 29 '14 at 5:24
1

You can check if you are in a category archive, and then get the current category being displayed and the get the last post from that category. In your category archive page (archive.php, category.php, index.php, ... whatever you use):

   if( is_category() ) {

    $cat = get_query_var('cat');
        $args = array(
                     'posts_per_page' => 1,
                     'cat'            => $cat,
                     'orderby'        => 'date',
                     'order'          => 'DESC',
                     'post_not_in'    => get_option( 'sticky_posts' )
        );

        $query = new WP_Query($args);

       if ( $query->have_posts() ) {  
            while( $query->have_posts() ) {  
                $query->the_post();      
                 get_template_part( 'content', 'latest' );
            }
        }

        wp_reset_postdata();
   }
  • That's great, @cybnet. It works perfectly for categories, and I guess I can apply the same principle to tag and search archive pages, using conditional statements or templates. Thanks! – markcbain May 24 '14 at 6:03
  • Exactly, you ca apply the same approach to tag, search, montly ... any archive-index page. Just use the correct conditionals. – cybmeta May 24 '14 at 8:32
  • Sorry, I see now that @unifiedac's answer was posted first, so I've changed my accepted answer. – markcbain May 29 '14 at 5:25
  • Oh, yes. I've never seen it. – cybmeta May 29 '14 at 7:20

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.