【问题标题】:Exclude current post from "Other posts of same category" loop从“同一类别的其他帖子”循环中排除当前帖子
【发布时间】:2015-05-03 16:11:29
【问题描述】:

我使用此代码在我的页面中显示相同类别的其他帖子:

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

如何从查询中排除客户正在查看的当前帖子,使其不会显示在“此类别的其他帖子”列表中?

非常感谢!

【问题讨论】:

  • 这个“该类别的其他帖子”从何而来?

标签: php wordpress post


【解决方案1】:

有一个参数“exclude”和get_posts

$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

?>

【讨论】:

  • 感谢您的回复,这看起来不错。如何获取当前的 $post->ID ?你能写下整个代码吗,这对你有用吗?很多很多,谢谢!
  • $post-&gt;ID 返回当前帖子 ID 。这就是为什么我要求您在 foreach 循环中使用另一个变量示例:foreach($posts as $p) 而不是 foreach($posts as $post)
  • 我已经按照您的指示编辑了代码,但现在,它根本不起作用。我犯了错误吗? (可以看到顶部的代码)
  • 因为您的 get_posts 参数没有被 array() 包裹。此外,要在你的 foreach 中获得标题,你应该这样做:$pz-&gt;post_title
  • 我复制 - 粘贴了您编辑的代码,但仍然没有运气:(。我会再检查一次。我认为 $title = $pz->post_title; 有问题
【解决方案2】:

您可以使用continue 跳过当前类别的循环。在循环中比较所选类别的 id 并跳过循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2021-03-15
    • 2013-01-21
    相关资源
    最近更新 更多