简体   繁体   English

在单个帖子页面上消除“该类别其他帖子”中的重复帖子

[英]Eliminate double posts in “other posts from this category” on single post page

I am using the following code to display posts from same category on a current post page: 我正在使用以下代码在当前帖子页面上显示相同类别的帖子:

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

foreach ($categories as $category[0]) {
if ($ceker == false){
    $ceker=true;    
    ?>
    <h3 class="naslovostalih">Other posts from this category:</h3>
    <ul class="clanciostalih">

    <?php

    $args = array(
        'numberposts' => 10,
        'category' => $category[0] -> 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, 'thumb-232');
                echo '<div id="excerptcu">';

                $pz = $pz->post_excerpt;
if (strlen($pz) > 160) {
$pz = substr($pz,0,strpos($pz,' ',160)); } ;
$pz = $pz . ' ...';
echo apply_filters('the_excerpt',$pz);
                echo '</div>';
                ?>
               <p class="more-link-wrapper2"><?php printf('<a class="read-more     button" title="%s" href="%s">Opširnije</a>', $title, $link, $title);?></p>
            </li>

        <?php } // end foreach ?>

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

It works almost perfect, but the problem is that Other posts are doubled so I have this kind of output: 它的工作原理几乎是完美的,但是问题是其他帖子加倍了,所以我有这种输出:

POST1 (which is in cat blizzard and in category europe) POST1(属于猫咪暴风雪和欧洲类别)

Content of POST1 POST1的内容

Other posts from this category: (should take just posts from the first category and that is blizzard) 此类别的其他帖子:(应该只接受第一类的帖子,那就是暴风雪)

Post2 POST2
Post3 Post3
Post4 Post4
Post2 POST2
Post3 Post3
Post4 Post4

So the "other posts" are doubled and I cant figure it out why. 因此,“其他职位”增加了一倍,我不知道为什么。

Any ideas? 有任何想法吗?

Foreach is the problem. Foreach是问题所在。 The first one. 第一个。 I managed to solve the issue by adding the break command after closing foreach. 我在关闭foreach之后通过添加break命令设法解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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