如何将帖子分配到类别

时间:2018-08-09 作者:William Jerome

我已经创建了自定义帖子类型,本月和下月创建了两个帖子类别,并为每个类别分配了示例帖子。预览类别存档时/分类/本月有一个页面未显示任何内容,因此我假设该页面存在,但帖子未显示。问题是,我如何才能使分配给帖子类别的帖子在say/category/this month上显示?

// Register Custom Post Type
function activity() {

    $labels = array(
        \'name\'                  => _x( \'Activity\', \'Post Type General Name\', \'twenty_two\' ),
        \'singular_name\'         => _x( \'Activity\', \'Post Type Singular Name\', \'twenty_two\' ),
        \'menu_name\'             => __( \'Activity\', \'twenty_two\' ),
        \'name_admin_bar\'        => __( \'Activity\', \'twenty_two\' ),
        \'archives\'              => __( \'Item Archives\', \'twenty_two\' ),
        \'attributes\'            => __( \'Item Attributes\', \'twenty_two\' ),
        \'parent_item_colon\'     => __( \'Parent Product:\', \'twenty_two\' ),
        \'all_items\'             => __( \'All Activity\', \'twenty_two\' ),
        \'add_new_item\'          => __( \'Add New Activity\', \'twenty_two\' ),
        \'add_new\'               => __( \'New Activity\', \'twenty_two\' ),
        \'new_item\'              => __( \'New Item\', \'twenty_two\' ),
        \'edit_item\'             => __( \'Edit Activity\', \'twenty_two\' ),
        \'update_item\'           => __( \'Update Activity\', \'twenty_two\' ),
        \'view_item\'             => __( \'View Activity\', \'twenty_two\' ),
        \'view_items\'            => __( \'View Items\', \'twenty_two\' ),
        \'search_items\'          => __( \'Search Activity\', \'twenty_two\' ),
        \'not_found\'             => __( \'No Activity found\', \'twenty_two\' ),
        \'not_found_in_trash\'    => __( \'No products found in Trash\', \'twenty_two\' ),
        \'featured_image\'        => __( \'Featured Image\', \'twenty_two\' ),
        \'set_featured_image\'    => __( \'Set featured image\', \'twenty_two\' ),
        \'remove_featured_image\' => __( \'Remove featured image\', \'twenty_two\' ),
        \'use_featured_image\'    => __( \'Use as featured image\', \'twenty_two\' ),
        \'insert_into_item\'      => __( \'Insert into item\', \'twenty_two\' ),
        \'uploaded_to_this_item\' => __( \'Uploaded to this item\', \'twenty_two\' ),
        \'items_list\'            => __( \'Items list\', \'twenty_two\' ),
        \'items_list_navigation\' => __( \'Items list navigation\', \'twenty_two\' ),
        \'filter_items_list\'     => __( \'Filter items list\', \'twenty_two\' ),
    );
    $args = array(
        \'label\'                 => __( \'Activity\', \'twenty_two\' ),
        \'description\'           => __( \'Display activities post\', \'twenty_two\' ),
        \'labels\'                => $labels,
        \'supports\'              => array( \'title\', \'editor\', \'thumbnail\' ),
        \'taxonomies\'            => array( \'category\', \'post_tag\' ),
        \'hierarchical\'          => false,
        \'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\'           => \'activities\',
        \'exclude_from_search\'   => false,
        \'publicly_queryable\'    => true,
        \'capability_type\'       => \'page\',
    );
    register_post_type( \'activity\', $args );

}
add_action( \'init\', \'activity\', 0 );

1 个回复
SO网友:Max Yudin

您必须为“活动”帖子类型注册内置的“类别”分类法:

<?php
function add_categories_to_activities()
{
    register_taxonomy_for_object_type( \'category\', \'activity\' );
}

add_action( \'init\', \'add_categories_to_activities\' );
See register_taxonomy_for_object_type() in the Code Reference.

结束

相关推荐

Organize functions.php

组织职能的最佳方式是什么。php的性能?我有几个add\\u操作调用、几个add\\u主题支持、几个add\\u过滤器和4个附加函数。对于面包屑、样式表、注册菜单和小部件。我们是否需要遵守订单?例如,首先是所有add\\u过滤器函数,然后是add theme\\u support等。不确定添加代码是否相关,因为这是一个一般性问题。如果需要,我很乐意更新这篇文章。