简体   繁体   中英

Exclude current post from “Other posts of same category” loop

I use this code to display other posts of same category in my page:

<?php 
global $post;
$categories = get_the_category();
$ceker=false;

foreach ($categories as $category) {
if ($ceker == false){
$ceker=true;    
?>
<h3 class="naslovostalih">Ostali članci iz ove kategorije:</h3>
<ul class="clanciostalih">
<?php

$args = array(
    'numberposts' => 10, 
    'offset'=> 1, 
    'category' => $category->term_id, 
    'exclude' => $post->ID
    );
}

$posts = get_posts($args);
    foreach($posts as $pz) { ?>
        <li>
            <?php
            $title = $pz->post_title;
            $link = get_permalink($pz->ID);
            printf('<a class="linkpost" title="%s" href="%s">%s</a>', $title, $link, $title);
            the_post_thumbnail('thumb-232');
            echo '<div id="excerptcu">';
            echo excerpt(25);
            echo '</div>';
            ?>
            <p class="more-link-wrapper2"><a href="<?php $link; ?>" class="read-more     button"><?php _e( 'Opširnije &raquo;', 'fearless' ); ?></a></p>
        </li>

    <?php } // end foreach ?>
</ul>

How can I exclude current post that a client is viewing from query, so it doesn't display in "Other posts from this category" list?

Many thanks!

There is a param " exclude " with get_posts

$args = array( 
    'numberposts' => 10, 'offset'=> 1, 
    'category' => $category->term_id, 
    'exclude' => $post->ID
);

$posts = get_posts($args);

Use another variable in your foreach() loop because it's ambiguous with global $post

Edit

<?php 
global $post;
$categories = get_the_category();
$ceker=false;

foreach ($categories as $category) {
if ($ceker == false){
    $ceker=true;    
    ?>
    <h3 class="naslovostalih">Ostali članci iz ove kategorije:</h3>
    <ul class="clanciostalih">
    <?php

    $args = array(
        'numberposts' => 10, 
        'offset'=> 1, 
        'category' => $category->term_id, 
        'exclude' => $post->ID
    );

    $posts = get_posts($args);
        foreach($posts as $pz) { ?>
            <li>
                <?php
                $title = $pz->post_title;
                $link = get_permalink($pz->ID);
                printf('<a class="linkpost" title="%s" href="%s">%s</a>', $title, $link, $title);
                echo get_the_post_thumbnail($pz->ID, 'thumbnail');
                echo '<div id="excerptcu">';
                echo substr($pz->post_content, 0, 25);
                echo '</div>';
                ?>
                <p class="more-link-wrapper2"><a href="<?php $link; ?>" class="read-more     button"><?php _e( 'Opširnije &raquo;', 'fearless' ); ?></a></p>
            </li>
        <?php } // end foreach ?>
    </ul>
<?php } // end if 
} //end foreach

?>

You can use continue to skip the loop of current category. Compare the selected category's id in the loop and skip the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM