0

First post on this site. Hope to get some help from you guys. Probably a simple answer for you experts.

I have two category's (news & work). I want to show on my index page (wordpress startpage/blog) the five latest news at the top (cat. news). Below that i want to show my post (cat news & work). I also have sticky posts in the work cat, that I want to only on the first page.

I think this means three loops. one for news, one for sticky in work & news, and then one for work & news, none sticky. I can get this to work, but the problem is that my sticky post shows up first on all the pages when I paginate on the index-file (eg. press the read prev post, /page/2, /page/3 etc..

How can I make the sticky post to be sticky only on the first page (index.php)?

Here is my code:

<div id="news">

<?php $wp_query = new WP_Query(array('category_name' => News, 'posts_per_page' => 5)); ?>

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    <?php get_template_part( 'loop', 'index' ); ?>

<?php endwhile; ?>

<?php wp_reset_postdata(); ?>

</div><!--#news-->



<div id="work">

<!-- Sticky -->

<?php if ( $paged != True ): ?>

<?php $wp_query = new WP_Query(array('post__in' => get_option('sticky_posts'), 'category_name' => Work)); ?>

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    <?php get_template_part( 'loop', 'index' ); ?>

<?php endwhile; ?>

<?php wp_reset_postdata(); ?>

<?php endif ?>





<!-- Non Sticky -->

<?php $wp_query = new WP_Query(array('post__not_in' => get_option('sticky_posts'), 'category_name' => Work, 'posts_per_page' => 8, 'paged' => $paged)); ?>

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

        <?php get_template_part( 'loop', 'index' ); ?>

    <?php endwhile; ?>

<?php wp_reset_postdata(); ?>

</div><!--#work-->

Thanks /K

My pagination code:

function pagination() {  
 global $wp_query;
 $total = $wp_query->max_num_pages;
 // Only paginate if we have more than one page
 if ( $total > 1 )  {
     // Get the current page
     if ( !$current_page = get_query_var('paged') )
          $current_page = 1;
     // Structure of “format” depends on whether we’re using pretty permalinks
    $permalinks = get_option('permalink_structure');
    $format = empty( $permalinks ) ? '&page=%#%' : 'page/%#%/';
    echo paginate_links(array(
          'base' => get_pagenum_link(1) . '%_%',
          'format' => $format,
          'current' => $current_page,
          'total' => $total,
          'mid_size' => 2,
          'prev_next' => False
    ));
}
}

1 Answer 1

0

Where you do your pagination, you can use something like if( $page != 1 ). This allows you you to output certain sections on only the first page. You will need to change $page to your pagination variable, but that is trivial.

8
  • Thanks for the quick reply. I added my pagination code in my question. I also added your code into mine, but there is still a sticky post on all pages.
    – Keat
    Commented Apr 15, 2012 at 16:33
  • Yes, but you didn't name the variable correctly. Change if ( $page != 1 ) to if ( $current_page != 1 ), so that you'll be working with a variable that is defined. Commented Apr 15, 2012 at 17:22
  • Oooops, sorry. I have changed it now, but there is still sticky posts on all the paginated pages. But I dont understand how that will change the display of Sticky posts. The Sticky post WP_Query have already been triggered before, and is written way before the pagination function is called.
    – Keat
    Commented Apr 15, 2012 at 17:45
  • @Keat The way I understood your question, the sticky posts were showing up in the non-sticky post query. Do you want the sticky posts query to not show up on the second page? Put whatever you want to only happen on the first page inside of the conditional (be it changing a query var, doing display, or anything else) and voilà!
    – mor7ifer
    Commented Apr 15, 2012 at 17:56
  • @BooneGorges The pagination code was not present when I posted my answer, I could not have known the variable name.
    – mor7ifer
    Commented Apr 15, 2012 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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