0

如何获取所有自定义帖子(post_type=family_guy)及其 ID 和多维数组中的所有标签?

这正是我希望我的数组看起来像的样子:

$array = array("1" =>   Array(
                           "Peter1",
                           "Lois1",
                           "Megan1"
                     ),  
          "2" =>   Array(
                           "Peter2",
                           "Lois2",
                           "Megan2"
                     ),
          "3" =>   Array(
                            "Peter3",
                            "Lois3",
                            "Megan3"
                     ),
         "4" =>   Array(
                            "Peter4",
                            "Lois4",
                            "Megan4"
                     )
          );

在此数组中,键将是自定义帖子 ID,值将是该自定义帖子的所有标签。

提前致谢。

4

1 回答 1

1

在functions.php中,这些方面的东西应该会有所帮助

function cpt_tag_list() {
    $types = get_post_types(array(
        '_builtin' => false  // This returns only custom post types
        'public' => true     // Not necessarily right, but consider this, you may need to filter out CPTs made by plugins.
    ));

    $op = array();
    $i = 0;

    foreach ($types as $cpt) {
        $i++;

        $posts_in_cpt = new WP_Query("posts_per_page=-1&post_type={$cpt}");

        $tags_used = array();

        foreach ($posts_in_cpt as $post) {
            $tags_used[] = get_tags($post->ID);
        }

        $op[(string) $i] = array_unique($tags_used);
    }

return $op
}

然后通过调用 cpt_tag_list(); 在您的站点中调用

抱歉,如果我在这方面犯了任何错误,我没有机会测试它,但希望它的要点对你有用。

于 2013-10-04T10:43:31.887 回答