Get assigned post categories

时间:2018-05-04 作者:Sam

我创建了一个名为“Crew”的自定义帖子类型,如下所示:

function crew_custom_post_type() {

  $labels = array(
    \'name\'                  => _x( \'Pages\', \'Post Type General Name\', \'project\' ),
    \'singular_name\'         => _x( \'Page\', \'Post Type Singular Name\', \'project\' ),
    \'menu_name\'             => __( \'Crew\', \'project\' ),
    \'name_admin_bar\'        => __( \'Crew\', \'project\' ),
    \'archives\'              => __( \'Item Archives\', \'project\' ),
    \'attributes\'            => __( \'Item Attributes\', \'project\' ),
    \'parent_item_colon\'     => __( \'Parent Item:\', \'project\' ),
    \'all_items\'             => __( \'All Items\', \'project\' ),
    \'add_new_item\'          => __( \'Add New Item\', \'project\' ),
    \'add_new\'               => __( \'Add New\', \'project\' ),
    \'new_item\'              => __( \'New Item\', \'project\' ),
    \'edit_item\'             => __( \'Edit Item\', \'project\' ),
    \'update_item\'           => __( \'Update Item\', \'project\' ),
    \'view_item\'             => __( \'View Item\', \'project\' ),
    \'view_items\'            => __( \'View Items\', \'project\' ),
    \'search_items\'          => __( \'Search Item\', \'project\' ),
    \'not_found\'             => __( \'Not found\', \'project\' ),
    \'not_found_in_trash\'    => __( \'Not found in Trash\', \'project\' ),
    \'featured_image\'        => __( \'Featured Image\', \'project\' ),
    \'set_featured_image\'    => __( \'Set featured image\', \'project\' ),
    \'remove_featured_image\' => __( \'Remove featured image\', \'project\' ),
    \'use_featured_image\'    => __( \'Use as featured image\', \'project\' ),
    \'insert_into_item\'      => __( \'Insert into item\', \'project\' ),
    \'uploaded_to_this_item\' => __( \'Uploaded to this item\', \'project\' ),
    \'items_list\'            => __( \'Items list\', \'project\' ),
    \'items_list_navigation\' => __( \'Items list navigation\', \'project\' ),
    \'filter_items_list\'     => __( \'Filter items list\', \'project\' ),
  );
  $args = array(
    \'label\'                 => __( \'Page\', \'project\' ),
    \'description\'           => __( \'Post Type for Crew\', \'project\' ),
    \'labels\'                => $labels,
    \'supports\'              => array( \'title\',\'author\' ),
    \'taxonomies\'            => array( \'crew\' ),
    \'hierarchical\'          => true,
    \'public\'                => true,
    \'show_ui\'               => true,
    \'show_in_menu\'          => true,
    \'menu_position\'         => 5,
    \'show_in_admin_bar\'     => true,
    \'show_in_nav_menus\'     => true,
    \'can_export\'            => true,
    \'has_archive\'           => true,
    \'exclude_from_search\'   => false,
    \'publicly_queryable\'    => true,
    \'capability_type\'       => \'page\',
  );
  register_post_type( \'crew\', $args );

}
add_action( \'init\', \'crew_custom_post_type\', 0 );
然后,我创建了一个分类法:

function crew_taxonomy() {

  $labels = array(
    \'name\'                       => _x( \'Categories\', \'Taxonomy General Name\', \'project\' ),
    \'singular_name\'              => _x( \'Category\', \'Taxonomy Singular Name\', \'sabsound\' ),
    \'menu_name\'                  => __( \'Categories\', \'sabsound\' ),
    \'all_items\'                  => __( \'All Items\', \'sabsound\' ),
    \'parent_item\'                => __( \'Parent Item\', \'sabsound\' ),
    \'parent_item_colon\'          => __( \'Parent Item:\', \'sabsound\' ),
    \'new_item_name\'              => __( \'New Item Name\', \'sabsound\' ),
    \'add_new_item\'               => __( \'Add New Item\', \'sabsound\' ),
    \'edit_item\'                  => __( \'Edit Item\', \'sabsound\' ),
    \'update_item\'                => __( \'Update Item\', \'sabsound\' ),
    \'view_item\'                  => __( \'View Item\', \'sabsound\' ),
    \'separate_items_with_commas\' => __( \'Separate items with commas\', \'sabsound\' ),
    \'add_or_remove_items\'        => __( \'Add or remove items\', \'sabsound\' ),
    \'choose_from_most_used\'      => __( \'Choose from the most used\', \'sabsound\' ),
    \'popular_items\'              => __( \'Popular Items\', \'sabsound\' ),
    \'search_items\'               => __( \'Search Items\', \'sabsound\' ),
    \'not_found\'                  => __( \'Not Found\', \'sabsound\' ),
    \'no_terms\'                   => __( \'No items\', \'sabsound\' ),
    \'items_list\'                 => __( \'Items list\', \'sabsound\' ),
    \'items_list_navigation\'      => __( \'Items list navigation\', \'sabsound\' ),
  );
  $args = array(
    \'labels\'                     => $labels,
    \'hierarchical\'               => true,
    \'public\'                     => true,
    \'show_ui\'                    => true,
    \'show_admin_column\'          => true,
    \'show_in_nav_menus\'          => true,
    \'show_tagcloud\'              => true,
  );
  register_taxonomy( \'crew\', array( \'crew\' ), $args );

}
add_action( \'init\', \'crew_taxonomy\', 0 );
我已经创建了一系列类别,并将它们分配给了crew自定义职位类型中的职位。

在归档页面上,我想为每个帖子输出指定的类别。

我在档案馆工作人员身上试过以下方法。php:

<?php

  // Begin loop
  while ( have_posts() ): the_post();

  // Get assigned categories for each post
  $post_id = get_the_ID();
  $array = array(
    \'taxonomy\' => \'crew\',
  );

  $categories = wp_get_post_categories( $post_id, $array );

?>


  <?php
    echo \'<h2>\'.get_the_title().\'</h2>\';
    echo \'<p>\'.$categories.\'</p>\';
  ?>


<?php // End loop
  endwhile;
?>
上面没有返回任何内容。我只是想列出要过滤的子类别,周围没有任何html。

1 个回复
最合适的回答,由SO网友:WebElaine 整理而成

wp_get_post_categories() 仅适用于核心类别,而不是自定义分类法。尝试wp_get_post_terms(). :)

结束

相关推荐

Taxonomy linked to pages

在我的产品页面中,我添加了许多自定义分类法。其中之一就是被称为“colecciones”的分类学。例如,您可以在此URL中看到:https://www.editorialufv.es/catalogo/comunicacion-clinica/ 每本书都是一个集合的一部分(西班牙语为Coleccion)。我希望产品页面中出现的链接将重定向到我对每个收藏所做的页面,而不是商店页面过滤器。在这种情况下,Coleccion被称为“relación clínica”,他的URL是:https://www.edit