• Anonymous User 7476186

    (@anonymized-7476186)


    How can i remove a given post placed in the page, from the loop placed just below?

    I have a box who display the last 3 posts from 3 different categories and i need to remove each one of them from the loop.

    How can i do that?

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • You should use the query_posts() function and set the “exclude” parameter to exclude the posts you’ve already displayed. That would look something like:

    <?php
    query_posts( array( 'exclude' => array( $first_post_id, $second_post_id, $third_post_id ) ) );
    ?>

    In the code above, the $first_post_id, $second_post_id and $third_post_id variables should be the IDs of the posts you need to exclude.

    Thread Starter Anonymous User 7476186

    (@anonymized-7476186)

    ok, but if i post a new article under one of the three categories obviously the post id will change. There is a generic way to esclude this three last posts?

    https://codex.www.ads-software.com/The_Loop#Multiple_Loops_in_Action

    particularly, the ‘array’ method of not duplicating posts;
    after this heading ‘Note for Multiple Posts in the First Category’

    Thread Starter Anonymous User 7476186

    (@anonymized-7476186)

    i noticed that my theme use this code to hide the post in the slider:
    <?php // Exclude the 5 featured posts from the loop for SEO purposes

    $exclude = get_option('mx_exclude_featured_posts');
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array('post__not_in' => $exclude, 'paged'=> $paged );
    query_posts($args);
    ?>

    Can i reuse that for my purpose?

    You could certainly use something similar, but you’d need to build your own $exclude variable. You could make the $exclude variable an array, and add each of the $post->ID properties from your featured posts into it.

    Thread Starter Anonymous User 7476186

    (@anonymized-7476186)

    I have no clue on how build the array, any hint?
    I should place the code after the loop?

    It should remove the 3 posts featured in the boxes (libri, news, extra) from the loop beneath. See link https://bit.ly/e85eWC

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Remove a post used in a page from the loop’ is closed to new replies.