如何从自定义帖子类型获取自定义类别URL?

时间:2012-06-14 作者:Dario Zadro

我正在使用自定义的帖子类型“公文包”来创建新的帖子和类别。我正在尝试从帖子中检索正确的类别/分类URL。我有几天要做这件事,真的需要一些帮助。注册邮局和税务:

function portfolioposttype() {

$labels = array(
    \'name\'               => __(\'Portfolio\'),
    \'singular_name\'      => __(\'Project\'),
    \'add_new\'            => __(\'Add New\'),
    \'add_new_item\'       => __(\'Add New Project\'),
    \'edit_item\'          => __(\'Edit Project\'),
    \'new_item\'           => __(\'New Project\'),
    \'view_item\'          => __(\'View Project\'),
    \'search_items\'       => __(\'Search Projects\'),
    \'not_found\'          => __(\'No projects found\'),
    \'not_found_in_trash\' => __(\'No projects found in Trash\'),
    \'parent_item_colon\'  => __(\'Parent Project:\'),
    \'menu_name\'          => __(\'Portfolio\')
);
$args = array(
    \'labels\'                => $labels,
    \'supports\'              => array(\'title\',\'editor\',\'thumbnail\'),
    \'taxonomies\'            => array(\'portfolio-categories\'),
    \'public\'                => true,
    \'show_ui\'               => true,
    \'show_in_menu\'          => true,
    \'show_in_nav_menus\'     => true,
    \'exclude_from_search\'   => true,
    \'has_archive\'           => true,
    \'rewrite\'               => array(\'slug\'=>\'case-study\'), //singe project slug
    \'capability_type\'       => \'post\',
    \'menu_position\'         => 5,
    \'menu_icon\'             => Z_BASE_URL.\'functions/assets/img/icon-portfolio.png\',
    \'query_var\'             => true
);
register_post_type(\'portfolio\', $args );

$taxonomy_portfolio_category_labels = array(
    \'name\'                       => __(\'Portfolio Categories\'),
    \'singular_name\'              => __(\'Category\'),
    \'search_items\'               => __(\'Search Categories\'),
    \'popular_items\'              => __(\'Popular Categories\'),
    \'all_items\'                  => __(\'All Categories\'),
    \'parent_item\'                => __(\'Parent Category\'),
    \'parent_item_colon\'          => __(\'Parent Category:\'),
    \'edit_item\'                  => __(\'Edit Category\'),
    \'update_item\'                => __(\'Update Category\'),
    \'add_new_item\'               => __(\'Add New Category\'),
    \'new_item_name\'              => __(\'New Category\'),
    \'separate_items_with_commas\' => __(\'Separate categories with commas\'),
    \'add_or_remove_items\'        => __(\'Add or remove Categories\'),
    \'choose_from_most_used\'      => __(\'Choose from most used Categories\'),
    \'menu_name\'                  => __(\'Categories\')
);
$taxonomy_portfolio_category_args = array(
    \'labels\'            => $taxonomy_portfolio_category_labels,
    \'public\'            => true,
    \'show_in_nav_menus\' => true,
    \'show_ui\'           => true,
    \'show_tagcloud\'     => false,
    \'hierarchical\'      => true,
    \'rewrite\'           => array(\'slug\'=>\'projects\'), //category slug
    \'query_var\'         => true     
);
register_taxonomy(\'portfolio-categories\',array(\'portfolio\'),$taxonomy_portfolio_category_args);

} 
add_action(\'init\',\'portfolioposttype\');
所以,从www.domain。com/案例研究/示例项目-1

我需要能够将其链接回类别:www.domain。com/项目/类别-1

可能还有另一个www.domain。com/案例研究/示例项目-2

这将链接到www.domain。com/项目/类别2

我希望这是有意义的,真的很感谢任何建议!

1 个回复
SO网友:Stephen Harris

一旦你有了帖子的期限(参见get_the_terms)您可以使用get_term_link 获取术语链接:

 $url = get_term_link(\'category-1\',\'portfolio-categories\');
可以传递术语对象、术语slug或术语ID。如果传递术语ID,则必须将其转换为整数。

结束

相关推荐