這應該爲你做,但首先有兩件事你應該知道。
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);
感謝您的幫助迄今。我使用了上面的代碼,並在我的自定義主題以及二十十六進行了測試。它適用於歸檔和單頁,但父頁面顯示爲歸檔。另外其他頁面(即聯繫頁面等)現在正在生成404錯誤。 –
它幾乎就在那裏......你有什麼建議可以嘗試嗎? –
這解決了404問題,但我仍然認爲可以將結構設置得更乾淨一些。也許是一種自定義分類法,而不是使用帖子的分類法。然後你可以使用ACF來形成關係。請參閱下面的更新,因爲它會告訴你如何改寫你的規則,你覺得合適。 https://gist.github.com/simplethemes/c9813cafef799000b04439ffae4a5c50 – simplethemes