Stopping duplicate content with PHP within a WordPress loop

Just when I thought I’d finished with all this php mallarky I spot another bug…

Basically I post a url, it spits it out. Sometimes I post the same url and I don’t want the duplicates to show up.

Read More

Here’s the code:

<?php
query_posts('');
while (have_posts()) : the_post();
$content = parse_url(strip_tags(get_the_content()));
$url = $content['host'];
?>

<li><a href="http://<?php echo $url; ?>"><?php echo $url; ?></a></li>

<?php endwhile;?>

I’ve tried modifying the loop to not fetch duplicate post ID’s but I can’t get it to work, either does nothing different or prints nothing at all. I’ve tried loading the $url into an array and then using array_unique() but again, no success, just spits out “Array Array Array” or errors. I’m sure it’s easier than I think, I just can’t get the code quite right I suspect…

I’d love some help please, I’ve been at this for a while and I’m all out of ideas

Related posts

Leave a Reply

2 comments

  1. I figured it out, well, someone kindly helped me 🙂

    <?php
        $url_list = array();
        query_posts('');
        while (have_posts()) : the_post();
        $content = parse_url(strip_tags(get_the_content()));
        $url = $content['host'];
        if(!in_array($url, $url_list)) {
    ?>
    
    <li><a href="http://<?php echo $url; ?>"><?php echo $url; ?></a></li>
    
    <?php
        $url_list[] = $url;
        }
    
    endwhile;
    

    Cheers,
    Craig.