在单个自定义 post 类型中显示所选类别 (Wordpress)
Showing selected categories in a single of a custom post type (Wordpress)
我创建了一个名为成员的自定义 post 类型:
// Custom post types
function members_post_type()
{
$args = array(
'labels' => array(
'name' => 'Members',
'singular_name' => 'Member',
'all_items' => 'All members'
),
// 'hierarchical' => true,
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor' , 'thumbnail'),
'menu_icon' => 'dashicons-groups'
);
register_post_type('members', $args);
}
add_action('init', 'members_post_type');
function members_taxonomys()
{
$args = array(
'public' => true,
'hierarchical' => true
);
register_taxonomy('Categories', array('members'), $args);
}
add_action('init', 'members_taxonomys');
并在单members.php
我制作了这段代码来调用我的类别:
<?php if(have_posts()):?>
<?php while(have_posts() ): ?>
<?php
the_category(' ');
?>
由于某种原因,它没有用。欢迎提出更多问题或更多详情。
- 您好,首先您需要更改自定义分类法的名称并为其添加标签:
function members_taxonomys()
{
$args = array(
'label' => 'My categories', // Label
'public' => true,
//'show_admin_column' => true,
'hierarchical' => true
);
// Custom category
register_taxonomy('my_categories', array('members'), $args);
}
add_action('init', 'members_taxonomys');
- 创建您自己的 post 类型时,您需要删除重写规则:
function members_post_type()
{
$args = array(
'labels' => array(
'name' => 'Members',
'singular_name' => 'Member',
'all_items' => 'All members'
),
// 'hierarchical' => true,
'public' => true,
'query_var' => true,
'rewrite' => true,
'has_archive' => true,
'supports' => array('title', 'editor' , 'thumbnail'),
'menu_icon' => 'dashicons-groups'
);
register_post_type('members', $args);
flush_rewrite_rules(false); // Remove rewrite rules
}
创建新成员post并为其分配自定义分类法
然后你需要替换the_category();
函数因为它适用于标准类别分类法,你需要使用get_the_terms函数
<?php if(have_posts()):?>
<?php while(have_posts() ): the_post(); ?>
<?php
$categories = get_the_terms($post, 'my_categories');
if(!empty($categories)) {
?><ul><?php
foreach($categories as $cat) {
print "<li>$cat->name</li>";
}
?></ul><?php
}
?>
<?php endwhile; ?>
<?php endif;?>
我创建了一个名为成员的自定义 post 类型:
// Custom post types
function members_post_type()
{
$args = array(
'labels' => array(
'name' => 'Members',
'singular_name' => 'Member',
'all_items' => 'All members'
),
// 'hierarchical' => true,
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor' , 'thumbnail'),
'menu_icon' => 'dashicons-groups'
);
register_post_type('members', $args);
}
add_action('init', 'members_post_type');
function members_taxonomys()
{
$args = array(
'public' => true,
'hierarchical' => true
);
register_taxonomy('Categories', array('members'), $args);
}
add_action('init', 'members_taxonomys');
<?php if(have_posts()):?>
<?php while(have_posts() ): ?>
<?php
the_category(' ');
?>
由于某种原因,它没有用。欢迎提出更多问题或更多详情。
- 您好,首先您需要更改自定义分类法的名称并为其添加标签:
function members_taxonomys()
{
$args = array(
'label' => 'My categories', // Label
'public' => true,
//'show_admin_column' => true,
'hierarchical' => true
);
// Custom category
register_taxonomy('my_categories', array('members'), $args);
}
add_action('init', 'members_taxonomys');
- 创建您自己的 post 类型时,您需要删除重写规则:
function members_post_type()
{
$args = array(
'labels' => array(
'name' => 'Members',
'singular_name' => 'Member',
'all_items' => 'All members'
),
// 'hierarchical' => true,
'public' => true,
'query_var' => true,
'rewrite' => true,
'has_archive' => true,
'supports' => array('title', 'editor' , 'thumbnail'),
'menu_icon' => 'dashicons-groups'
);
register_post_type('members', $args);
flush_rewrite_rules(false); // Remove rewrite rules
}
创建新成员post并为其分配自定义分类法
然后你需要替换
the_category();
函数因为它适用于标准类别分类法,你需要使用get_the_terms函数
<?php if(have_posts()):?>
<?php while(have_posts() ): the_post(); ?>
<?php
$categories = get_the_terms($post, 'my_categories');
if(!empty($categories)) {
?><ul><?php
foreach($categories as $cat) {
print "<li>$cat->name</li>";
}
?></ul><?php
}
?>
<?php endwhile; ?>
<?php endif;?>