[英]Wordpress Custom Post Type rewrite page and category slug
我已經設置了自定義帖子類型
register_post_type( 'Communities',
array(
'labels' => array(
'name' => __( 'Communities' ),
'singular_name' => __( 'Community' ),
'not_found' => __( 'No Communities Found' )
),
'public' => true,
'rewrite' => array( 'slug' => '/%category%/communities', 'with_front' => false),
'has_archive' => true,
'hierarchical' => true,
'taxonomies' => array( 'category' ),
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ),
'menu_icon' => 'dashicons-admin-home'
)
);
我有一個帶有城市名稱的頁面設置,出於各種原因,我需要將其保留為頁面,但我希望將該城市中的社區存儲為自定義帖子類型。 我為要添加社區的每個城市設置了類別,但是我希望他們將城市名稱頁面視為父級。
像這樣
www.sitename.com/cityname/communities/communityname/
我希望cityname是一個頁面,community是一個存檔頁面,而communityname是一個頁面。
到目前為止,我發現的每個解決方案都會產生404錯誤或與父頁面沖突。
任何幫助表示贊賞。 謝謝!
這應該為您做,但是首先您應該知道兩件事。
1.)我不建議使用此URL結構,因為它要求每個條目至少具有一個類別。 它將始終搶占陣列中的第一個。 您需要編寫一個小插件,以告知您的作者。 這可能會達到目的:
https://srd.wordpress.org/plugins/require-post-category/
2.)在查看帖子之前,添加代碼后,您需要訪問設置>永久鏈接,以強制進行重寫刷新。
/**
* Register Community Custom Post Type
*/
function community_post_type() {
$labels = array(
'name' => 'Communities',
'singular_name' => 'Community',
'menu_name' => 'Communities',
'name_admin_bar' => 'Communities',
'archives' => 'Community Archives',
'attributes' => 'Community Attributes',
'parent_item_colon' => 'Parent Community:',
'all_items' => 'All Communities',
'add_new_item' => 'Add New Community',
'add_new' => 'Add New',
'new_item' => 'New Community',
'edit_item' => 'Edit Community',
'update_item' => 'Update Community',
'view_item' => 'View Community',
'view_items' => 'View Communities',
'search_items' => 'Search Communities',
'not_found' => 'Community Not found',
'not_found_in_trash' => 'Not found in Trash',
'featured_image' => 'Featured Image',
'set_featured_image' => 'Set featured image',
'remove_featured_image' => 'Remove featured image',
'use_featured_image' => 'Use as featured image',
'insert_into_item' => 'Insert into item',
'uploaded_to_this_item' => 'Uploaded to this Community',
'items_list' => 'Communities list',
'items_list_navigation' => 'Communities list navigation',
'filter_items_list' => 'Filter Communities list',
);
$rewrite = array(
'slug' => '/%category%/communities',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => 'Community',
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
'taxonomies' => array( 'category' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-universal-access',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
'show_in_rest' => true,
);
register_post_type( 'communities', $args );
}
add_action( 'init', 'community_post_type', 0 );
/**
* Add category slug to community post links
* @param $post_link
* @param $id
*/
function my_commmunity_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) && $post->post_type == 'communities' ){
$terms = wp_get_object_terms( $post->ID, 'category' );
if( !empty($terms) ){
return str_replace( '%category%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'my_commmunity_post_link', 1, 3 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.