Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Jeal

    (@jeal)

    Turns out this worked great, but had a pretty serious bug.

    The sidebar is on (almost) all the pages, so when you’re viewing an individual post the first two latest news items don’t show up, ever.

    so in my sidebar .php, right before the news category loop, I needed a way to offset manually. I started by getting the post_id before the loop even started:

    wp_reset_query(); //reset
    $current_post = $post->ID;

    Then within the loop:

    //if, while, the...
    <?php $current_query = $post->ID; ?>
    <?php if (  $current_post == $current_query ){
    continue; //skip the post, display nothing
    } ?>
    //the post name & link
    <?php endwhile; endif ?>

    The latest news checks if the current post is part of the query and skips it if it does.

    This results in one less post than I want, so I added code to add a certain amount to post_per_page if the current page is a news item.

    Additionally, I needed to add code to modify the offset if I’m on a news blog feed page, that defaults to null.

    $post_per_page = 5; //default count
    if ( is_category( 16 ) ) //if news category
    $posts_per_page = 6; //display 6 posts because one will be skipped
    $offset = null; //default offset
    if ( is_page( '123' ) ) //is this the news page?
    $offset = 2;
    $args = array(
    'posts_per_page' => $post_per_page,
    'offset' => $offset
    ... //more conditions
    );
    $latest_news_sidebar = new WP_Query( $args );( 
    
    //the loop

    Thanks again for the help. I just thought I’d write that up for my own good and anyone who stumbles onto this page.

    Edit: Still having some issues… :\

    Thread Starter Jeal

    (@jeal)

    I wasn’t aware of an offset option :).

    I was also thinking the less queries the better, right? Seems like a waste resources to do the same thing twice. In my case, its a small site with little traffic, but still no reason to build inefficiently.

    That sounds like it will work though, thanks

Viewing 2 replies - 1 through 2 (of 2 total)