Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Because of the way my templates are structured, I need to pull the first/sticky post from the site outside of the standard loop, just on the homepage. I'm using _s (underscores.me) as a base, so the current loop on the index look like this:

    <?php if ( have_posts() ) : ?>

  <?php /* Start the Loop */ ?>
  <?php while ( have_posts() ) : the_post(); ?>

    <?php
      /* Include the Post-Format-specific template for the content.
       * If you want to overload this in a child theme then include a file
       * called content-___.php (where ___ is the Post Format name) and that will be used instead.
       */
      get_template_part( 'content', get_post_format() );
    ?>

  <?php endwhile; ?>

<?php elseif ( current_user_can( 'edit_posts' ) ) : ?>

  <?php get_template_part( 'no-results', 'index' ); ?>

<?php endif; ?>

What do I have to do to query the first post (I'd like to use the same get_template_part construct if possible), on the homepage only, and not have it interfere with counts for paging past the homepage?

Thanks,

UPDATE

Next part of the question; I now want to add a custom query inbetween the separated first post, and the continuation below, that only pulls a few posts in from specific categories. My query doesn't seem to do anything - what's wrong with it? Are those two main loops interferring with it somehow?

    <?php
$args = array(
    'cat' => 6521,//(int) - use category id.
    'category_name' => 'featured', 'dossier', 'destacado',
);    
$featured_posts = new WP_Query( $args );

// The Loop
if ( $featured_posts->have_posts() ) :

while ( $featured_posts->have_posts() ) : $featured_posts->the_post();
  echo '<div style="background-color:#f00;">';
  the_title();
  echo '</div>';
endwhile;

endif;

// Reset Post Data
wp_reset_postdata();
?>

Thanks,

share|improve this question
    
I added an update to my answer below. –  Simon Sep 19 '12 at 8:40
add comment

2 Answers

try and add this before your code:

<?php if ( have_posts() && is_home() && !is_paged() ) : ?>
    <?php the_post(); ?>
    <?php get_template_part( 'content', get_post_format() ); ?>
<?php endif; ?>
share|improve this answer
add comment

If I understand correctly you want to fetch the first post using a separate query, perhaps in order to distinguish it from the rest of the posts in your markup. To accomplish that, I'd try something like this:

<?php global $paged;
<?php if (is_home() && !$paged) : ?>

  <?php
  $posts_per_page = get_option('posts_per_page');
  $num_featured_posts = 1;

  query_posts(array('posts_per_page' => $num_featured_posts)); ?>

  <div id="featured">

    <?php if ( have_posts() ) : ?>

      <?php while ( have_posts() ) : the_post(); ?>

        <?php get_template_part( 'content', get_post_format() ); ?>

      <?php endwhile; ?>

    <?php elseif ( current_user_can( 'edit_posts' ) ) : ?>

      <?php get_template_part( 'no-results', 'index' ); ?>

    <?php endif; ?>

  </div>

  <?php
    wp_reset_query();
    query_posts(array('posts_per_page' => $posts_per_page - $num_featured_posts, 'offset' => $num_featured_posts));
  ?>

<?php endif; ?>

<div id="posts">

  <?php if ( have_posts() ) : ?>

    <?php while ( have_posts() ) : the_post(); ?>

      <?php get_template_part( 'content', get_post_format() ); ?>

    <?php endwhile; ?>

  <?php elseif ( current_user_can( 'edit_posts' ) ) : ?>

    <?php get_template_part( 'no-results', 'index' ); ?>

  <?php endif; ?>

</div>

Regarding your update

I think you have an error in your query. First of all, you are using both cat and category_name which do the same thing using different parameters (id vs. slug). This is probably not much of an issue though, since one will probably take precedence.

I think the issue is that you are passing multiple slugs as the category_name parameter, while it takes only one. Instead, use category__in and pass category IDs in an array like so:

$args = array(
   'category__in' => array(1, 2, 3)
);

Also, when you are having issues with WP_Query it might be a good idea to add wp_reset_query() calls before and after your query to make sure it's not interfering with surrounding queries, add in some of the most common query variables like post_type, posts_per_page, paged etc. to make sure they are not being overridden elsewhere. Lastly, sometimes just printing the query object using print_r($featured_posts); will give you valuable debugging information.

share|improve this answer
    
Looks just about right - I'll try it out later tonight and see what happens. Thanks for the quick reply! –  Luc Pestille Sep 18 '12 at 9:11
1  
That worked perfectly (so far), thanks. –  Luc Pestille Sep 18 '12 at 22:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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