No repeat posts ( in sidebar ) (4 posts)

  1. venkmanuk
    Posted 4 years ago #

    Hi there,

    I'm building a custom theme. My sidebar shows 'adverts' that lead to a post in category 5 and the main column shows all other posts and categories except category 5.

    Here's the thing:

    When someone clicks on the post ( 'advert' ) in the sidebar and we go to that post's 'single' page, how can i prevent the post being shown in the sidebar?

    i think the code needs to say:
    if - anything in the sidebar loop = the current page id, skip it.

    does this sound right? how do i do that in php?

  2. Michael
    Forum Moderator
    Posted 4 years ago #

    what code is used to show the sidebar posts?

    (please follow http://codex.wordpress.org/Forum_Welcome#Posting_Code when posting code)


    look into the 'post__not_in' parameter for queries;
    also, read http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action for avoiding duplicate posts;

    only guessing: you might need to use a conditional tag such as is_single(), and add global $post; before the code.

  3. venkmanuk
    Posted 4 years ago #

    thanks for your reply,
    here's a pastebin of the current code:

    maybe it'll work to add this! ?

    function myPostid() {
    global $wp_query;
    $thePostID = $wp_query->post->ID;
     if ( $post->ID == $thePostID ) continue;
  4. venkmanuk
    Posted 4 years ago #

    This still isn't working for me .. and i'm trying to use it in a new project now!

    the code in question is:
    if ( $post->ID == $thePostID ) continue; ?>

    even if i set up a variable in the main loop that finds $post->ID and then the sidebar compares it to $post->ID it doesnt work. it either completely breaks or just allows repeat posts. :/

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

Topic Closed

This topic has been closed to new replies.

About this Topic