gonefussin Posted March 4, 2012 Share Posted March 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258244-duplicate-content-from-my-loop/ Share on other sites More sharing options...
litebearer Posted March 4, 2012 Share Posted March 4, 2012 did you try putting them all into an array then use http://php.net/manual/en/function.array-unique.php Quote Link to comment https://forums.phpfreaks.com/topic/258244-duplicate-content-from-my-loop/#findComment-1323746 Share on other sites More sharing options...
gonefussin Posted March 4, 2012 Author Share Posted March 4, 2012 Hi Lite, Yea I tried, but I don't think I got the code right. It was just spitting out "Array Array Array ...". If you think that's what'll work I'll try again, if you've any tips that'd be great. Thanks, Craig. Quote Link to comment https://forums.phpfreaks.com/topic/258244-duplicate-content-from-my-loop/#findComment-1323757 Share on other sites More sharing options...
scootstah Posted March 4, 2012 Share Posted March 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258244-duplicate-content-from-my-loop/#findComment-1323767 Share on other sites More sharing options...
gonefussin Posted March 4, 2012 Author Share Posted March 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258244-duplicate-content-from-my-loop/#findComment-1323784 Share on other sites More sharing options...
scootstah Posted March 4, 2012 Share Posted March 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258244-duplicate-content-from-my-loop/#findComment-1323835 Share on other sites More sharing options...
gonefussin Posted March 4, 2012 Author Share Posted March 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/258244-duplicate-content-from-my-loop/#findComment-1323849 Share on other sites More sharing options...
gonefussin Posted March 4, 2012 Author Share Posted March 4, 2012 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/258244-duplicate-content-from-my-loop/#findComment-1323866 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.