問題描述
有較早的帖子有類似的標題,但它不會看到 WordPress 3.3,這是重要的 3.3 廣告有趣的是:「使用 postname 永久連結結構沒有效能損失」
WordPress 3.2 及更早版本的問題是,首先它看起來是頁面名稱,然後是 404 。它沒有首先檢查任意的帖子型別。 3.3 另一方面,必須看後期型別,然後頁面,最後 404(因為它通告這個功能) 。這意味著沒有 slug 的自定義帖子型別應該是簡單的,如果他們沒有硬編碼 post_type=post 在某個地方。
我找不到 3.3 具體的解決方案。
問題:如何為任何給定的自定義帖子型別”xyz” 定義永久連結結構”/%postname%/”?
謝謝。
最佳解決方案
這不容易在 WP 3.3 中完成,除非你將重寫規則置於正確的位置,並使 wp_rewrite 認為在前端使用了詳細規則。下面的課程是工作。
class Test_Post_Type {
const POST_TYPE = 'test';
public static function init() {
global $wp_rewrite;
$post_type_obj = register_post_type( self::POST_TYPE, array(
'labels' => array(
'name' => __( 'Tests' ),
'singular_name' => __( 'Test' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Test' ),
'edit_item' => __( 'Edit Test' ),
'new_item' => __( 'New Test' ),
'all_items' => __( 'All Tests' ),
'view_item' => __( 'View Test' ),
'search_items' => __( 'Search Tests' ),
'not_found' => __( 'No Tests found' ),
'not_found_in_trash' => __( 'No Tests found in Trash' ),
'menu_name' => __( 'Tests' )
),
'publicly_queryable' => true,
'exclude_from_search' => true,
'hierarchical' => false,
'public' => true,
'rewrite' => false,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'thumbnail', 'test_source' ),
'taxonomies' => array( 'category', 'post_tag' ),
) );
$post_type_obj = get_post_type_object(self::POST_TYPE);
//register the rewrite tag for permalink building
$wp_rewrite->add_rewrite_tag( '%' . $post_type_obj->query_var . '%', '([^/]+)', $post_type_obj->query_var . '=' );
//we have to add the permastruct here in order to build the permalink, otherwise we'll need to filter the post_type link
add_permastruct(self::POST_TYPE, '%' . $post_type_obj->query_var . '%/', false );
//add a filter to remove the permastructs generated above
add_filter(self::POST_TYPE . '_rewrite_rules', array(__CLASS__, '_remove_default_rules'));
//now we add a filter to put the generated rewrite rules in the correct spot
add_action('generate_rewrite_rules', array(__CLASS__, '_filter_rewrite_rules'));
if(!is_admin()) {
//we need verbose_page_rules to be on on the front end in order for pages to be process properly
$wp_rewrite->use_verbose_page_rules = true;
}
}
/**
* Filter to remove the rules for this post type when they're automatically generated due to the permastruct registration
* @param type $rules
* @return type
*/
public static function _remove_default_rules($rules) {
return array();
}
/**
* Filters the rules at the end to add back the ones for this post type at the bottom
* @param WP_Rewrite $wp_rewrite
*/
public static function _filter_rewrite_rules($wp_rewrite) {
$post_type_obj = get_post_type_object(self::POST_TYPE);
$my_rules = $wp_rewrite->generate_rewrite_rules('%' . $post_type_obj->query_var . '%', EP_NONE);
$wp_rewrite->rules += $my_rules;
}
}
add_action( 'init', array( 'Test_Post_Type', 'init' ) );
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。