在Wordpress循环中使用PHP停止重复内容

在Wordpress循环中使用PHP停止重复内容,php,wordpress,Php,Wordpress,就在我以为我已经完成了所有这些php mallarky的时候,我发现了另一个bug 基本上我发布了一个url,它会把它吐出来。有时我发布相同的url,我不想重复出现 代码如下: <?php query_posts(''); while (have_posts()) : the_post(); $content = parse_url(strip_tags(get_the_content())); $url = $content['host']; ?> <li><a

就在我以为我已经完成了所有这些php mallarky的时候,我发现了另一个bug

基本上我发布了一个url,它会把它吐出来。有时我发布相同的url,我不想重复出现

代码如下:

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

  • 我试图修改循环以不获取重复的帖子ID,但我无法使其工作,或者没有任何不同,或者根本没有打印任何内容。我尝试将$url加载到数组中,然后使用array_unique(),但同样没有成功,只是抛出了“array”或错误。我确信这比我想象的要容易,我只是不能完全正确地理解代码,我怀疑


    我很想得到一些帮助,我已经做了一段时间了,我已经没有主意了。为什么不看看Wordpress在过去的几个版本中实现的WP_查询选项呢?我总是发现,通过包含数组之类的东西,这些选项是更有用的方法

    您可以在此处查看法典:


    虽然不是一个直接的答案,但它可能会引导你在探索中找到一个明确的答案

    我明白了,嗯,有人好心地帮助了我:)

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