1
votes

I'm displaying a article on my page in Wordpress. Then right below that article, Im doing a foreach loop and displaying latest articles. Here is how it looks:

enter image description here

As you can see, "Jeffs Wedding Surprise" displays again in the bottom articles. How can I make it so the same article wont display again in my foreach loop that I'm viewing right now.

Here is how Im looping through the "Similar articles" at the bottom and my featured article:

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

2 Answers

1
votes

You can add condition to check the title/id of main article not display in similar articles.

In main article code

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

For similar articles

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

This is what I was supposed to do in the foreach loop:

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

That will exclude the current article in the "Similar articles" section on bottom of page.