自定义发布分层结构到自定义分类

时间:2014-08-23 作者:Grady D

我创建了一个自定义帖子类型和一个自定义分类法。问题是,我无法将自定义帖子与它所在的类别进行分层。我有以下代码,

function AppManager_custom_post() {
  $labels = array(
    \'name\'               => _x( \'APKS\', \'post type general name\' ),
    \'singular_name\'      => _x( \'APK\', \'post type singular name\' ),
    \'add_new\'            => _x( \'Add New APK\', \'APK\' ),
    \'add_new_item\'       => __( \'Add New APK\' ),
    \'edit_item\'          => __( \'Edit APK\' ),
    \'new_item\'           => __( \'New APK\' ),
    \'all_items\'          => __( \'All APKs\' ),
    \'view_item\'          => __( \'View APK\' ),
    \'search_items\'       => __( \'Search APKs\' ),
    \'not_found\'          => __( \'No APK found\' ),
    \'not_found_in_trash\' => __( \'No APKs found in the Trash\' ),
  );
  $args = array(
    \'labels\'        => $labels,
    \'description\'   => \'Holds Apps and meta data\',
    \'public\'        => true,
    \'menu_position\' => 5,
    \'supports\'      => array( \'title\', \'thumbnail\', \'excerpt\' ),
    \'has_archive\'   => true,
      \'hierarchical\'  => true,
    \'rewrite\'       => array(\'slug\' => \'APKs\')
  );
  register_post_type( \'apps_post\', $args );
  flush_rewrite_rules();
}
add_action( \'init\', \'AppManager_custom_post\' );
我有以下内容来创建分类法,

function AppManager_custom_taxonomy() {
  $labels = array(
    \'name\'              => _x( \'App Categories\', \'taxonomy general name\' ),
    \'singular_name\'     => _x( \'App Category\', \'taxonomy singular name\' ),
    \'search_items\'      => __( \'Search App Categories\' ),
    \'all_items\'         => __( \'All App Categories\' ),
    \'parent_item\'       => __( \'Parent App Category\' ),
    \'parent_item_colon\' => __( \'Parent App Category:\' ),
    \'edit_item\'         => __( \'Edit App Category\' ),
    \'update_item\'       => __( \'Update App Category\' ),
    \'add_new_item\'      => __( \'Add New App Category\' ),
    \'new_item_name\'     => __( \'New App Category\' ),
    \'menu_name\'         => __( \'App Categories\' ),
  );
  $args = array(
    \'labels\'       => $labels,
    \'hierarchical\' => true,
    \'rewrite\'      => array( \'hierarchical\' => true, \'slug\' => \'Apps\' )
  );
  register_taxonomy( \'appCategory\', \'apps_post\', $args );

}
add_action( \'init\', \'AppManager_custom_taxonomy\', 0 );
无论我如何尝试层次函数,它都不会映射到我添加到的类别。有可能吗?

2 个回复
SO网友:Pieter Goosen

这可能无法回答您的问题,但您的代码存在一些问题

函数名称、分类法名称和自定义帖子类型名称中不要使用大写字母。仅使用小写字母。用下划线分隔名称(_). 还有一个提示,不要以字母开头,也不要使用连字符(-) 分隔名称的步骤

切勿使用flush_rewrite_rules(); 以你所做的方式。这是一项极其昂贵的操作。目前,它将在每次页面加载时加载。这将大大增加您的加载时间,这对于SEO来说是不好的。检查法典是否正确使用。

不要创建连接到同一挂钩的多个函数。创建一个函数,并将该函数挂钩到所需的挂钩

SO网友:Hassan Alvi

进行以下修改,我希望它能完成工作

function AppManager_custom_taxonomy() {
  $labels = array(
    \'name\'              => _x( \'App Categories\', \'taxonomy general name\' ),
    \'singular_name\'     => _x( \'App Category\', \'taxonomy singular name\' ),
    \'search_items\'      => __( \'Search App Categories\' ),
    \'all_items\'         => __( \'All App Categories\' ),
    \'parent_item\'       => __( \'Parent App Category\' ),
    \'parent_item_colon\' => __( \'Parent App Category:\' ),
    \'edit_item\'         => __( \'Edit App Category\' ),
    \'update_item\'       => __( \'Update App Category\' ),
    \'add_new_item\'      => __( \'Add New App Category\' ),
    \'new_item_name\'     => __( \'New App Category\' ),
    \'menu_name\'         => __( \'App Categories\' ),
  );
  $args = array(
    \'label\'        => $labels,
    \'rewrite\'      => array( \'slug\' => \'Apps\' ),
    \'hierarchical\' => true
  );
  register_taxonomy( \'appCategory\', \'apps_post\', $args );

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

结束

相关推荐