簡體   English   中英

從“其他相同類別的帖子”循環中排除當前帖子

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

我使用以下代碼在頁面中顯示相同類別的其他帖子:

<?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>

如何從查詢中排除客戶正在查看的當前帖子,這樣它就不會顯示在“此類別的其他帖子”列表中?

非常感謝!

get_posts有一個參數“ exclude

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

$posts = get_posts($args);

在您的foreach()循環中使用另一個變量,因為它與全局$post不明確

編輯

<?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

?>

您可以使用continue跳過當前類別的循環。 在循環中比較所選類別的ID,然后跳過循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM