简体   繁体   中英

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)

Content of POST1

Other posts from this category: (should take just posts from the first category and that is blizzard)

Post2
Post3
Post4
Post2
Post3
Post4

So the "other posts" are doubled and I cant figure it out why.

Any ideas?

Foreach is the problem. The first one. I managed to solve the issue by adding the break command after closing foreach.

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