我是wordpress的新手,我已经全面了解了CPT和自定义分类法的法典,以及一些托里亚托里亚语。显然我遗漏了一些东西,但是由于我一直只能通过分类法获得404,所以我无法弄清楚。是的,我一直要进行永久链接,然后单击“保存更改”以重置缓存。
这是我当前的设置:
我有一个CPT作为“人”,一个分类法是“活动”。这些活动是在metabox中预定义的,所以我有游泳者,跑步者,骑自行车者。
数据正在保存,我在不同的分类法中分别创建了3个人进行测试,因此对任何分组的调用都应产生1条信息。
我要完成的工作是让我的网址显示分类法名称,从而过滤结果:
mydomain.com/people/swimmer
mydomain.com/people/runner
等等...
我能够获得CPT的列表
mydomain.com/people
我可以得到单个帖子:
mydomain.com/people/person-title-field
我想用代码而不是插件来解决此问题-我更喜欢“边做边学”的方法,因为从长远来看,这是有回报的。
这是来自functions.php文件的代码。如果有人能告诉我我所缺少的内容或配置不正确,将不胜感激。
function create_people_post_type()
{
$labels = array(
'name' => __( 'People' ),
'singular_name' => __( 'Person' ),
'search_items' => __("Search People"),
'all_items' => __("All People"),
'parent_item' => __("Parent Person"),
'parent_item_colon' => __("Parent Person"),
'edit_item' => __("Edit Person"),
'update_item' => __("Update Person"),
'add_new_item' => __("Add New Person"),
'new_item_name' => __("New Person's Name")
);
$args = array(
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 6,
'has_archive' => true
);
register_post_type( 'people', $args);
}
add_action('init', 'create_people_post_type');
function create_activities_taxonomy()
{
$labels = array(
'name' => __( 'activities' ),
'singular_name' => __( 'activity' ),
'search_items' => __("Search activities"),
'all_items' => __("All activities"),
'parent_item' => __("Parent activity"),
'parent_item_colon' => __("Parent activity"),
'edit_item' => __("Edit activity"),
'update_item' => __("Update activity"),
'add_new_item' => __("Add New activity"),
'new_item_name' => __("New activity's Name")
);
$args = array(
'public' => true,
'show_ui' => false,
'show_in_nav_menus' => false,
'hierarchical' => true,
'query_var' => false,
'labels' => $labels,
'has_archive' => true
);
register_taxonomy('activities','people', $args);
}
add_action('init', 'create_activities_taxonomy');
function activities_metabox()
{
add_meta_box(
'activities_box_id',
'Activities',
'activities_custom_box_html',
'people',
'side',
'low'
);
}
add_action('add_meta_boxes', 'activities_metabox');
function activities_custom_box_html($post)
{
wp_nonce_field( plugin_basename(__FILE__), 'activities_custom_box_html_nonce');
$value = get_post_meta($post->ID, '_activities_meta_key', true);
?>
<label for="activities_field">Select Department</label>
<select name="activities_field" id="activities_field" class="postbox">
<option value="runner" <?php selected($value, 'runner'); ?>>Runner</option>
<option value="swimmer" <?php selected($value, 'swimmer'); ?>>Swimmer</option>
<option value="cyclist" <?php selected($value, 'cyclist'); ?>>Cyclist</option>
</select>
<?php
}
function activities_save_postdata($post_id)
{
if (array_key_exists('activities_field', $_POST)) {
update_post_meta(
$post_id,
'_activities_meta_key',
$_POST['activities_field']
);
}
}
add_action('save_post', 'activities_save_postdata');
提前谢谢!