【问题标题】:Hide duplicate post on article page - Wordpress隐藏文章页面上的重复帖子 - Wordpress
【发布时间】:2017-03-29 14:41:56
【问题描述】:

我在我的 Wordpress 页面上显示一篇文章。然后在那篇文章的正下方,我正在做一个 foreach 循环并显示最新文章。这是它的外观:

如您所见,“Jeffs Wedding Surprise”再次显示在底部文章中。我怎样才能使同一篇文章不会再次显示在我现在正在查看的 foreach 循环中。

这是我如何循环浏览底部的“类似文章”和我的特色文章:

<?php if( have_posts() ){
            while( have_posts(['posts_per_page'=>100]) ){
                $articles = the_post();
                $thumb_id = get_post_thumbnail_id($articles);
                $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
                $thumb_url = $thumb_url_array[0];
        ?>
            <?php } ?>
            <div class="article-heading">
                <h4><?php the_title(); ?></h4>
            </div>
            <div class="article-background">
                <img src="<?=$thumb_url;?>">
                <?php
                $title = get_the_title();
                $content = the_content();
                if(strlen($content)>0){?>
                    <div class="article-details">
                        <?php $content ?>
                    </div>
                <?php } ?>
            </div>
        <?php } ?>


        <div class="similar-section">
            <h3>Similar Articles</h3>
        </div>
        <?php foreach (get_posts(['post_type' => 'add_article', 'posts_per_page'=>3]) as $articles) {
            $thumb_id = get_post_thumbnail_id($articles);
            $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail-size', true);
            $thumb_url = $thumb_url_array[0];
            $url_to_project = "/previous_work/".$articles->post_name;
            ?>
                <div class="col-md-4">
                    <a class="article-link" href="<?=$url_to_project;?>">
                        <div class="single-article-photo">
                            <img src="<?=$thumb_url;?>">
                        </div>
                        <div class="single-article">
                            <h6><?= $articles->post_title ?></h6>
                            <p><?= substr($articles->post_content,0,200) ?> <span style="text-transform: uppercase; text-decoration: underline">(Read More)</span>...</p>
                        </div>
                    </a>
                </div>
        <?php } ?>

【问题讨论】:

    标签: php wordpress foreach


    【解决方案1】:

    您可以添加条件来检查主文章的标题/ID在类似文章中不显示。

    在主文章代码中

    $title = get_the_title(); // title of main article
    

    类似文章

     foreach (get_posts(['post_type' => 'add_article', 'posts_per_page'=>3]) as $articles) {
        if($title!=$articles->post_title){
          // display article 
        }
    }
    

    【讨论】:

      【解决方案2】:

      这是我在 foreach 循环中应该做的:

      <?php foreach (get_posts(['post_type' => 'add_article', 'posts_per_page'=>3,'exclude'=>[get_the_ID()]]) as $articles) { ?>
      

      这将排除页面底部“类似文章”部分中的当前文章。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-26
        • 1970-01-01
        • 1970-01-01
        • 2021-08-16
        相关资源
        最近更新 更多