admin管理员组文章数量:1332697
The below code is not working. I wanted to use add_rewrite_rule
wordpress function for multiple post types but it works if i use it once.
I want some one who can make this code work.
function my_add_rewrite_rules() {
add_rewrite_tag('%organization-post%', '([^/]+)', 'organization-post=');
add_rewrite_rule('^([^/]+)/([^/]+)/?', 'index.php?gb-organization-post=$matches[2]', 'top');
add_rewrite_tag('%course%', '([^/]+)');
add_rewrite_rule('^([^/]+)/([^/]+)/?', 'index.php?gb-course=$matches[2]', 'top');
add_rewrite_tag('%news%', '([^/]+)');
add_rewrite_rule('^([^/]+)/([^/]+)/?', 'index.php?gb-news=$matches[2]', 'top');
//flush_rewrite_rules();
}
add_action( 'init', 'my_add_rewrite_rules' );
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'organization-post';
$query_vars[] = 'course';
$query_vars[] = 'news';
return $query_vars;
} );
function my_permalinks($permalink, $post, $leavename) {
$post_id = $post->ID;
if( ($post->post_type != 'gb-organization-post' && $post->post_type != 'gb-course' && $post->post_type != 'gb-news' ) || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))){
return $permalink;
}
$parent = $post->post_parent;
$parent_post = get_post( $parent );
if($post->post_type == 'gb-organization-post' && $parent_post->post_name != ''){
$tab_name = get_post_meta($post_id,'gb_tab_name', true);
$permalink = str_replace('%organization-post%', $parent_post->post_name, $permalink);
}
if($post->post_type == 'gb-course' && $parent_post->post_name != ''){
$tab_name = get_post_meta($post_id,'gb_tab_name', true);
$permalink = str_replace('%course%', $parent_post->post_name, $permalink);
}
if($post->post_type == 'gb-news' && $parent_post->post_name != ''){
$tab_name = get_post_meta($post_id,'gb_tab_name', true);
$permalink = str_replace('%news%', $parent_post->post_name, $permalink);
}
return $permalink;
}
add_filter('post_type_link', 'my_permalinks', 10, 3);
Here is My Register Post types Code
if ( !function_exists('gb_add_organization_post_types') ) {
function gb_add_organization_post_types() {
$labels = array(
'name' => _x( 'Organizations', 'Post type general name', 'gb-organization' ),
'singular_name' => _x( 'Organization', 'Post type singular name', 'gb-organization' ),
'menu_name' => _x( 'Organizations', 'Admin Menu text', 'gb-organization' ),
'name_admin_bar' => _x( 'Organization', 'Add New on Toolbar', 'gb-organization' ),
'add_new' => __( 'Add New', 'gb-organization' ),
'add_new_item' => __( 'Add New Organization', 'gb-organization' ),
'new_item' => __( 'New Organization', 'gb-organization' ),
'edit_item' => __( 'Edit Organization', 'gb-organization' ),
'view_item' => __( 'View Organization', 'gb-organization' ),
'all_items' => __( 'All Organizations', 'gb-organization' ),
'search_items' => __( 'Search Organizations', 'gb-organization' ),
'parent_item_colon' => __( 'Parent Organizations:', 'gb-organization' ),
'not_found' => __( 'No organizations found.', 'gb-organization' ),
'not_found_in_trash' => __( 'No organizations found in Trash.', 'gb-organization' ),
'featured_image' => _x( 'Organization Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'set_featured_image' => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'use_featured_image' => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'archives' => _x( 'Organization archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'gb-organization' ),
'insert_into_item' => _x( 'Insert into organization', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'gb-organization' ),
'uploaded_to_this_item' => _x( 'Uploaded to this organization', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'gb-organization' ),
'filter_items_list' => _x( 'Filter organizations list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'gb-organization' ),
'items_list_navigation' => _x( 'Organizations list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'gb-organization' ),
'items_list' => _x( 'Organizations list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'gb-organization' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'organization' ),
'taxonomies' => array('post_tag','category'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => 200,
'menu_icon' => 'dashicons-businessman',
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
);
register_post_type( 'gb-organization', $args );
//Organization Posts
$labels = array(
'name' => _x( 'Organization Posts', 'Post type general name', 'gb-organization' ),
'singular_name' => _x( 'Organization Post', 'Post type singular name', 'gb-organization' ),
'menu_name' => _x( 'Organization Posts', 'Admin Menu text', 'gb-organization' ),
'name_admin_bar' => _x( 'Organization Post', 'Add New on Toolbar', 'gb-organization' ),
'add_new' => __( 'Add New', 'gb-organization' ),
'add_new_item' => __( 'Add New Organization Post', 'gb-organization' ),
'new_item' => __( 'New Organization Post', 'gb-organization' ),
'edit_item' => __( 'Edit Organization Post', 'gb-organization' ),
'view_item' => __( 'View Organization Post', 'gb-organization' ),
'all_items' => __( 'All Organization Posts', 'gb-organization' ),
'search_items' => __( 'Search Organization Posts', 'gb-organization' ),
'parent_item_colon' => __( 'Parent Organization Posts:', 'gb-organization' ),
'not_found' => __( 'No organizations found.', 'gb-organization' ),
'not_found_in_trash' => __( 'No organizations found in Trash.', 'gb-organization' ),
'featured_image' => _x( 'Organization Post Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'set_featured_image' => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'use_featured_image' => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'archives' => _x( 'Organization Post archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'gb-organization' ),
'insert_into_item' => _x( 'Insert into organization', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'gb-organization' ),
'uploaded_to_this_item' => _x( 'Uploaded to this organization', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'gb-organization' ),
'filter_items_list' => _x( 'Filter organizations list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'gb-organization' ),
'items_list_navigation' => _x( 'Organization Posts list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'gb-organization' ),
'items_list' => _x( 'Organization Posts list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'gb-organization' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => '%organization-post%' ),
'capability_type' => 'post',
'taxonomies' => array('post_tag','category'),
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 200,
'menu_icon' => 'dashicons-list-view',
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
);
register_post_type( 'gb-organization-post', $args );
//Caurses
$labels = array(
'name' => _x( 'Courses', 'Post type general name', 'gb-organization' ),
'singular_name' => _x( 'Course', 'Post type singular name', 'gb-organization' ),
'menu_name' => _x( 'Courses', 'Admin Menu text', 'gb-organization' ),
'name_admin_bar' => _x( 'Course', 'Add New on Toolbar', 'gb-organization' ),
'add_new' => __( 'Add New', 'gb-organization' ),
'add_new_item' => __( 'Add New Course', 'gb-organization' ),
'new_item' => __( 'New Course', 'gb-organization' ),
'edit_item' => __( 'Edit Course', 'gb-organization' ),
'view_item' => __( 'View Course', 'gb-organization' ),
'all_items' => __( 'All Courses', 'gb-organization' ),
'search_items' => __( 'Search Courses', 'gb-organization' ),
'parent_item_colon' => __( 'Parent Courses:', 'gb-organization' ),
'not_found' => __( 'No courses found.', 'gb-organization' ),
'not_found_in_trash' => __( 'No courses found in Trash.', 'gb-organization' ),
'featured_image' => _x( 'Course Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'set_featured_image' => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'use_featured_image' => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'archives' => _x( 'Course archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'gb-organization' ),
'insert_into_item' => _x( 'Insert into course', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'gb-organization' ),
'uploaded_to_this_item' => _x( 'Uploaded to this course', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'gb-organization' ),
'filter_items_list' => _x( 'Filter courses list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'gb-organization' ),
'items_list_navigation' => _x( 'Courses list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'gb-organization' ),
'items_list' => _x( 'Courses list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'gb-organization' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => '%course%' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'taxonomies' => array('post_tag','category'),
'menu_position' => 200,
'menu_icon' => 'dashicons-welcome-learn-more',
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
);
register_post_type( 'gb-course', $args );
//News
$labels = array(
'name' => _x( 'News', 'Post type general name', 'gb-organization' ),
'singular_name' => _x( 'News', 'Post type singular name', 'gb-organization' ),
'menu_name' => _x( 'News', 'Admin Menu text', 'gb-organization' ),
'name_admin_bar' => _x( 'News', 'Add New on Toolbar', 'gb-organization' ),
'add_new' => __( 'Add New News', 'gb-organization' ),
'add_new_item' => __( 'Add New News', 'gb-organization' ),
'new_item' => __( 'New News', 'gb-organization' ),
'edit_item' => __( 'Edit News', 'gb-organization' ),
'view_item' => __( 'View News', 'gb-organization' ),
'all_items' => __( 'All News', 'gb-organization' ),
'search_items' => __( 'Search News', 'gb-organization' ),
'parent_item_colon' => __( 'Parent News:', 'gb-organization' ),
'not_found' => __( 'No newss found.', 'gb-organization' ),
'not_found_in_trash' => __( 'No newss found in Trash.', 'gb-organization' ),
'featured_image' => _x( 'News Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'set_featured_image' => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'use_featured_image' => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'gb-organization' ),
'archives' => _x( 'News archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'gb-organization' ),
'insert_into_item' => _x( 'Insert into news', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'gb-organization' ),
'uploaded_to_this_item' => _x( 'Uploaded to this news', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'gb-organization' ),
'filter_items_list' => _x( 'Filter newss list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'gb-organization' ),
'items_list_navigation' => _x( 'News list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'gb-organization' ),
'items_list' => _x( 'News list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'gb-organization' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => '%news%' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'taxonomies' => array('post_tag','category'),
'menu_position' => 200,
'menu_icon' => 'dashicons-megaphone',
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
);
register_post_type( 'gb-news', $args );
}
}
add_action( 'init', 'gb_add_organization_post_types' );
Thanks in Advance :)
本文标签: functionsWordpress I wanted to use Add rewrite rule multiple times for multiple CPT in my plugin
版权声明:本文标题:functions - Wordpress: I wanted to use Add rewrite rule multiple times for multiple CPT in my plugin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742338986a2456212.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论