【发布时间】:2014-08-28 07:29:30
【问题描述】:
我的网站上有一个名为 Homes 的自定义帖子类型,其中包含一个名为 home-category 的自定义分类。它们都正常工作,但我在使用固定链接时遇到了问题。我需要永久链接是家庭/家庭类别/页面名称。我写了一个重写函数,所以每当我转到一个家庭项目时,永久链接都会正确显示,但我在页面本身上得到一个 404。我不确定是什么原因造成的,也不知道如何解决它。如果没有固定链接中的自定义分类法,房屋项目可以正常工作,但不能使用它。有没有人对如何解决这个问题有任何想法?我一直在寻找没有运气的日子。
这是我的自定义帖子类型的代码:
register_post_type( 'Homes',
array(
'labels' => array(
'name' => __( 'Homes' ),
'singular_name' => __( 'Homes Item' ),
'add_new_item' => __('Add New Homes Item'),
'edit_item' => __('Edit Homes Item'),
'new_item' => __('New Homes Item'),
),
'supports' => array('title', 'thumbnail', 'editor'),
'taxonomies' => array('homes-category'),
'public' => true,
'has_archive' => false,
'show_in_nav_menus' => TRUE,
'show_in_menu' => TRUE,
'rewrite' => array(
'slug' => 'homes/%homes-category%',
'with_front' => true,
'hierarchical' => true,
),
)
);
这是我的自定义分类的代码
register_taxonomy(
'homes-category',
'homes',
array(
'hierarchical' => true,
'label' => __( 'Availability Category' ),
'rewrite' => array(
'slug' => 'homes',
'with_front' => false,
),
)
);
这里是重写函数
function custom_post_link($post_link, $id = 0)
{
$post = get_post($id);
if(!is_object($post) || $post->post_type != 'homes')
{
return $post_link;
}
$homes = 'misc';
if($terms = wp_get_object_terms($post->ID, 'homes-category'))
{
$client = $terms[0]->slug;
//Replace the query var surrounded by % with the slug of
//the first taxonomy it belongs to.
return str_replace('%homes-category%', $homes, $post_link);
}
//If all else fails, just return the $post_link.
return $post_link;
}
add_filter('post_type_link', 'custom_post_link', 1, 3);
【问题讨论】:
标签: wordpress custom-post-type permalinks taxonomy