2012-01-04 68 views
0

在wordpress中是否有一个函数遍历所有帖子并放弃同名的帖子?例如。我有几个帖子命名为“香蕉”,我只需要一个帖子显示该名称?WordPress的+过滤器的名称

它实际上是自定义帖子类型,我使用WP_Query来查询帖子?

谢谢, 彼得

回答

0

我不相信有这样的功能。但是您可以轻松地将此功能添加到香草WP_Query循环中。下面是一个使用数组迭代过程中“记住”的名字后一个解决方案:

<?php 

    // The Query 
    $the_query = new WP_Query($args); 

    // an array to remember titles 
    $titles = array() 

    // The Loop 
    while ($the_query->have_posts()) : $the_query->the_post(); 

     $the_title = get_the_title(); // (1) grab the current post title 
     if ($titles[$the_title]) continue; // (2) duplicate title: skip post! 
     $titles[$the_title] = TRUE; // (3) otherwise, remember the title 

     // ..do post stuff.. 

    endwhile; 

    // Reset Post Data 
    wp_reset_postdata(); 

?> 

为(2)是快的,因为Array作为PHP中的地图实现的查找。有关更多信息,请参阅Arrays

+0

绝妙的主意。我使用了类似的东西......我只是将所有东西都放到数组中,而不是比较数组来排除重复项(我的项目比我描述的要复杂一些,但是你给了我一个如何解决它的想法)。谢了哥们 ! – Peter 2012-01-08 02:44:41