我有一个自定义的帖子类型“目标”设置。为每个目标分配常规职位,导致像http://example.com/credit/fix-my-credit/learn-how (格式为%category%/%goal%/%postname%
) 其中:
类别(slug)=“信用”(credit)目标(slug)=“修复我的信用”(fix my credit)
贴子(slug)=“了解如何”(learn how) $labels = array(
\'name\' => _x( \'Goals\', \'Post Type General Name\', \'bonestheme\' ),
\'singular_name\' => _x( \'Goals\', \'Post Type Singular Name\', \'bonestheme\' ),
\'menu_name\' => __( \'Goals\', \'bonestheme\' ),
\'name_admin_bar\' => __( \'Goals\', \'bonestheme\' ),
\'archives\' => __( \'Goal Archives\', \'bonestheme\' ),
\'parent_item_colon\' => __( \'Parent Goal:\', \'bonestheme\' ),
\'all_items\' => __( \'All Goals\', \'bonestheme\' ),
\'add_new_item\' => __( \'Add New Goal\', \'bonestheme\' ),
\'add_new\' => __( \'Add New Goal\', \'bonestheme\' ),
\'new_item\' => __( \'New Goal\', \'bonestheme\' ),
\'edit_item\' => __( \'Edit Goal\', \'bonestheme\' ),
\'update_item\' => __( \'Update Goal\', \'bonestheme\' ),
\'view_item\' => __( \'View Goal\', \'bonestheme\' ),
\'search_items\' => __( \'Search Goals\', \'bonestheme\' ),
\'not_found\' => __( \'Not found\', \'bonestheme\' ),
\'not_found_in_trash\' => __( \'Not found in Trash\', \'bonestheme\' ),
\'featured_image\' => __( \'Featured Image\', \'bonestheme\' ),
\'set_featured_image\' => __( \'Set featured image\', \'bonestheme\' ),
\'remove_featured_image\' => __( \'Remove featured image\', \'bonestheme\' ),
\'use_featured_image\' => __( \'Use as featured image\', \'bonestheme\' ),
\'insert_into_item\' => __( \'Insert into item\', \'bonestheme\' ),
\'uploaded_to_this_item\' => __( \'Uploaded to this item\', \'bonestheme\' ),
\'items_list\' => __( \'Items list\', \'bonestheme\' ),
\'items_list_navigation\' => __( \'Items list navigation\', \'bonestheme\' ),
\'filter_items_list\' => __( \'Filter items list\', \'bonestheme\' ),
);
$args = array(
\'label\' => __( \'Goals\', \'bonestheme\' ),
\'description\' => __( \'Post Type Description\', \'bonestheme\' ),
\'labels\' => $labels,
\'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'custom-fields\', \'comments\' ),
\'taxonomies\' => array( \'category\', \'tag\' ),
\'hierarchical\' => true,
\'public\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'menu_position\' => 5,
\'show_in_admin_bar\' => true,
\'show_in_nav_menus\' => true,
\'can_export\' => true,
\'has_archive\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'capability_type\' => \'page\',
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'goal\', \'with_front\' => false ),
);
重写:
function custom_rewrite_basic() {
add_rewrite_rule(
\'^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?\',
\'index.php?category_name=$matches[1]&goal=$matches[2]&goal_article=$matches[3]\',
\'top\'
);
}
add_action(\'init\', \'custom_rewrite_basic\');
Permalink过滤器:
function update_article_permalink( $permalink, $post_id ) {
global $post;
if ( get_post_type($post_id) === \'post\' ) {
$the_goal = get_article_goal($post_id);
$post = get_post($post_id);
$post->post_name = sanitize_title($post->post_title);
$post_link = $permalink;
if (!empty($the_goal)) {
$goal_slug = $the_goal[\'slug\'];
$post_link = str_replace( \'%goal%\', $goal_slug, $permalink );
} else {
$slug = $post->post_name;
$post_link = str_replace( \'%goal%/\', \'\', $permalink );
}
$permalink = $post_link;
}
if ( get_post_type($post_id) === \'goal\' ) {
$categories = get_the_category($post_id);
if (count($categories) > 0){
$first_cat = $categories[0]->slug;
$permalink = preg_replace( \'(\\/goal\\/)\', \'/\'.$first_cat.\'/\', $permalink );
}
}
return $permalink;
}
add_filter( \'get_sample_permalink\', \'update_article_permalink\', 10, 2 );
我错过了什么?我怎样才能
%goal%
是否在所有permalinks中替换?