2017-03-31 1 views
0

は、私はさまざまな理由のためのページとして維持する必要がある都市名を持つページを設定しているカスタムポストタイプWordpressのカスタムポストタイプの書き換えページやカテゴリスラッグ

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' 
    ) 
); 

を設定しますが、私していますその都市のコミュニティをカスタムの投稿タイプとして保存するようにします。私はコミュニティを追加している都市ごとにカテゴリを設定していますが、citynameページを親として扱いたいと思います。この

www.sitename.com/cityname/communities/communityname/ 

よう

私はコミュニティがアーカイブページであること、およびcommunitynameが単一のページであることを、citynameページになりたいです。

私が今までに見つけたすべての解決策は、404エラーまたは親ページとの競合を生成します。

何か助けていただければ幸いです。ありがとう!

答えて

1

これはあなたのために行うべきですが、まず知っておくべき2つのことがあります。

1.すべてのエントリに少なくとも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); 
+0

感謝。私は上記のコードを使用し、私のカスタムテーマとtwentysixteenでそれをテストしました。これは、アーカイブと単一ページには効果的ですが、親ページはアーカイブとして表示されます。また、他のページ(連絡先ページなど)で404エラーが発生しています。 –

+0

それはほぼそこにあるようです...あなたは私が試みることができる任意の提案がありますか? –

+0

これは404の問題を修正しましたが、私はまだ構造が少しきれいに設定できると思います。ポストのタクソノミーを使用する代わりに、カスタムタクソノミを使用することもあります。次に、ACFを使用して関係を形成することができます。下のアップデートを見ると、ルールがどのようにフィットするかを書き直す方法が示されます。 https://gist.github.com/simplethemes/c9813cafef799000b04439ffae4a5c50 – simplethemes

関連する問題