Jump to content

Duplicate content from my loop


gonefussin

Recommended Posts

Hi. I'm new to PHP and Wordpress, quite the beginner. I thought I finally had it all done on my site and I've stumbled on another issue.

 

Basically I post a link and it spits it out. I sometimes post links that have the same domain ($content['host']) but I don't want these to be duplicated.

 

Here's what I have:

 

<?php query_posts(''); ?>
<?php while (have_posts()) : the_post(); ?>

<li>
<a href="http://<?php $content = parse_url(strip_tags(get_the_content())); echo $content['host']; ?>">

<?php $content = parse_url(strip_tags(get_the_content())); echo $content['host']; ?></a>
</li>

<?php endwhile;?>

 

I've Googled around for a while, reading posts about how to prevent duplicate posts in multiple loops but can't adapt it to mine. I'd really appreciate any advice.

 

Cheers,

Craig.

Link to comment
Share on other sites

You'll have to modify the have_posts() function to not fetch duplicates. Or, modify the code that saves the data to not save duplicates.

 

Hi Scoots,

 

Could you elaborate on that please? I've tried stopping it via have_posts with "if($post->ID == $do_not_duplicate) continue; ?>" but it either doesn't stop the duplicates coming out or outputs nothing at all. I don't know what you mean by the code that saves the data.

 

Am I on the right track with this:

<?php query_posts(''); ?>
<?php while (have_posts()) : the_post();

$s = array(the_content());
$d = array_unique($s); ?>

<li><a href="http://<?php $content = parse_url(strip_tags($d)); echo $content['host']; ?>"><?php $content = parse_url(strip_tags($d)); echo $content['host']; ?></a></li>

<?php endwhile;?>

 

Thanks,

Craig.

Link to comment
Share on other sites

array_unique won't work from inside the while loop, since it's only getting one row at a time.

 

Is it actually duplicating rows or do you just have more than one row with the same content? If the latter, you should be able to, on each iteration, add the link to an array. Then on at the start of the loop, check if the current link already exists in the array. If it does, use continue to stop the loop and go to the next post.

Link to comment
Share on other sites

I had a feeling it wouldn't work like that, I've been looking for ways to add it to an array and then just echo the array at the end once the loop is done but still can't figure it out.

 

Yea, I think you're right, it's not so much duplicating them as posting two with the same content. Like I said, I've been trying to add the content to an array but I can't get it to work, Google's failing me too.. I know how to create an array with the data already defined at the beginning but can't figure out how to add stuff to an existing array. Would I then check the array with array_unique(), scoots?

 

Cheers,

Craig.

Link to comment
Share on other sites

Think I got it :) A guy over at webdesignerforum helped me out, for anyone intersted here's the fix:

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.