2017-03-29 48 views
1

我在Wordpress的頁面上顯示文章。然後就在那篇文章的下面,我正在做一個foreach循環並顯示最新的文章。下面是它的外觀:在文章頁面上隱藏重複的文章 - Wordpress

enter image description here

正如你所看到的,「傑夫斯婚禮驚喜」在底部的文章再次顯示。我怎樣才能讓它在我現在正在查看的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 } ?> 

回答

0

這是我應該在foreach循環中要做到:

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

這將排除當前文章在「相似的文章」 secti在頁面的底部。

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 
    } 
} 
相關問題