[英]permalink not working on custom post and category
我正在嘗試在wordpress中添加wordpress自定義帖子類型和類別
add_action( 'init', 'work_register' );
function work_register() {
$labels = array(
'name' => _x('Work', 'post type general name'),
'singular_name' => _x('Work Item', 'post type singular name'),
'add_new' => _x('Add New', 'work item'),
'add_new_item' => __('Add New Work Item'),
'edit_item' => __('Edit Work Item'),
'new_item' => __('New Work Item'),
'view_item' => __('View Work Item'),
'search_items' => __('Search Work'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
'rewrite' => array( 'slug' => 'work', 'with_front'=> false ),
'capability_type' => 'post',
'hierarchical' => true,
'has_archive' => true,
'menu_position' => null,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'work' , $args );
register_taxonomy( 'categories', array('work'), array(
'hierarchical' => true,
'label' => 'Categories',
'singular_label' => 'Category',
'rewrite' => array( 'slug' => 'categories', 'with_front'=> false )
)
);
register_taxonomy_for_object_type( 'categories', 'work' );
}
我的永久鏈接結構是:
/%category%/%postname%/
當我從帖子中添加內容時,它給了我鏈接
/category/postname
但是當我從自定義帖子中添加內容時,它只會給我
/postname
我也想在自定義帖子中使用/ category / postname
請指出我錯了
更改您的重寫以添加工作崗位類型查詢var:
'rewrite' => array('slug' => 'work/%categories%')
然后過濾post_type_link
以將所選類別插入永久鏈接:
function custom_work_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'work' );
if( $terms ){
return str_replace( '%categories%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'custom_work_post_link', 1, 3 );
還有一些插件,例如“ 自定義帖子類型”永久鏈接可以為您完成此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.