Wordpress +过滤帖子的名称

时间:2012-01-04 18:10:12

标签: wordpress filter custom-post-type

在wordpress中是否有一个函数来遍历所有帖子并丢弃具有相同名称的帖子?例如。我有几个名为“Banana”的帖子,我只需要一个帖子显示该名称?

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

谢谢, 彼得

1 个答案:

答案 0 :(得分:0)

我不相信有这样的功能。但您可以轻松地将此功能添加到vanilla 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