web-dev-qa-db-ja.com

書き換えルールが衝突したときの問題?

カスタム分類法とカスタム投稿タイプで同じ書き換え構造を使用するのは問題ですか。

カスタム分類法peopleとカスタム投稿タイプpeople_bioがあります。アイデアは、あなたがページの上に短い略歴を持つ人についての投稿のリストを得るということです。私はそれらを私のtaxonomy-people.phpテンプレートファイルにまとめました。パーマリンクは/people/[person-slug]です。

カスタム分類法とカスタム投稿タイプの両方で、rewrite引数がarray('slug' => 'people')に設定されています。これはうまくいくようです:get_term_link('seth-godin', 'people')/people/seth-godin/を返し、slug seth-godinを持つカスタム投稿タイプの場合、get_permalink()/people/seth-godin/を返します。分類法が最初に定義され、それは "勝つ"ようです:/people/[slug]ページでは、is_tax()はtrueですが、is_single()はfalseです。

それで、それはうまくいきます、しかし私はそれに慣れていません。誰かが書き換えエンジンをより経験していますか、そして、これが他のことを壊すかもしれないかどうか私に言うことができますか?


プラグインファイルの関連部分。initアクションで呼び出されます。

register_taxonomy(
    'people',
    'post',
    array(
        'labels' => array(
            'name' => 'People',
            'singular_name' => 'Person',
            'search_items' => 'Search people',
            'popular_items' => 'Popular people',
            'all_items' => 'All people',
            'parent_item' => null,
            'parent_item_colon' => null,
            'edit_item' => 'Edit person',
            'update_item' => 'Update person',
            'add_new_item' => 'Add person',
            'new_item_name' => 'New person',
            'separate_items_with_commas' => 'Separate people with commas',
            'add_or_remove_items' => 'Add or remove people',
            'choose_from_most_used' => 'Choose from the most used people',
        ),
        'public' => true,
        'show_ui' => true,
        'show_tagcloud' => true,
        'hierarchical' => false,
        'update_count_callback' => '',
        'rewrite' => array(
            'slug' => 'people',
            'with_front' => true,
        ),
        'query_var' => 'people',
        'capabilities' => array(),
        'show_in_nav_menus' => true,
    )
);


register_post_type(
    'people_bio',
    array(
        'label' => 'People Bio',
        'labels' => array(
            'name' => 'Biographies',
            'singular_name' => 'Biography',
            'add_new' => 'Add new',
            'add_new_item' => 'Add new biography',
            'edit_item' => 'Edit biography',
            'new_item' => 'New biography',
            'view_item' => 'View biography',
            'search_items' => 'Search biographies',
            'not_found' => 'No biographies found',
            'not_found_in_trash' => 'No biographies found in trash',
            'parent_item_colon' => null,
        ),
        'description' => 'Biography pages of interesting people',
        'public' => true,
        'exclude_from_search' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'menu_position' => null,
        'menu_icon' => null,
        'capability_type' => 'post',
        'capabilities' => array(),
        'hierarchical' => false,
        'supports' => array(
            'title',
            'editor',
            //'author',
            'thumbnail',
            'excerpt',
            //'trackbacks',
            'custom-fields',
            //'comments',
            //'revisions',
            //'page-attributes',
        ),
        'register_meta_box_cb' => null,
        'taxonomies' => array(),
        'permalink_epmask' => EP_PERMALINK,
        //'rewrite' => false,
        'rewrite' => array(
            'slug' => 'people',
            'with_front' => true,
        ),
        'query_var' => true,
        'can_export' => true,
        'show_in_nav_menus' => true,
    )
);

register_taxonomy_for_object_type('people', 'people_bio');

(私はCodexが最新でない限り、私はいつもregister_*()を持つすべてのパラメータを、デフォルト値を持つ多くのものを特別なドキュメントとして使用します)


taxonomy-people.phpテンプレートファイル:

<?php
get_header();

$people_biography = get_posts(array(
    'numberposts' => -1,
    'post_type' => 'people_bio',
    'taxonomy' => 'people',
    'term' => $wp_query->get_queried_object()->slug,
));
?>
<div class="container_24">
    <div class="grid_18" id="content" role="main">
        <?php if ($people_biography) :
            foreach ($people_biography as $bio) : ?>
                <h1><?php echo get_the_title($bio->ID); ?></h1>
                <?php
                echo get_the_post_thumbnail($bio->ID);
                echo apply_filters('the_content', $bio->post_content); ?>
        <?php
            endforeach;
         else: ?>
            <h1><?php esc_html_e($wp_query->get_queried_object()->name); ?></h1>
        <?php endif; ?>
        <?php get_template_part( 'loop', 'archive' ); ?>
    </div><!-- .content -->

    <div class="grid_6" id="default_sidebar">
        <?php dynamic_sidebar('default-sidebar'); ?>
    </div><!-- #default_sidebar -->
</div><!-- .container_24 -->
<div class="clear"></div>
<?php
get_footer();
?>

更新:生成された書き換え規則

私の Rewrite Analyzer の出力は、通常の分類ページに対して分類法が「勝つ」(これは私が気付いたものです)と言っているようですが、カスタム投稿タイプ他のすべてのURL(2ページ目、フィードなど)を取得します。これは私が恐れていたことであり、私はさらに調査する必要があるでしょう。 Rewrite rules for people taxonomy and post type

3
Jan Fabry

はい、これは問題を引き起こします。カスタム分類法によって生成される4つの書き換え規則は、カスタム投稿タイプでも使用されます。最初に登録したものが、その後に登録したものによって上書きされます。正確な設定(階層的なものと非階層的なもの)によっては、同様の規則が最終的な書き換え規則の一覧に含まれることがありますが、最初のものだけが "勝つ"ことができます。

これは、/people/seth-godin/がカスタム分類法を照会するのに対し、/people/seth-godin/page/2/はカスタム投稿タイプを照会するという効果があります。

1
Jan Fabry

質問:カスタム分類法とカスタム投稿タイプで同じ書き換え構造を使用するのは問題ですか?

答え:はい。

それらがまったく同じ構造を共有している場合は、競合状態になるので、そのうちの1つは機能しません。しかし、私はあなたのシナリオが自動的に同じ書き換え規則につながるわけではないと思います。

1
hakre

これは、(post_typeと分類法の登録によって作成されたものを含む)permastructure書き換え規則がarray_merge()を介して実行されるために起こります。 array_mergeは、配列の現在位置にあるrewrite_rulesを同じ正規表現に置き換えますが、競合しないものは配列の末尾に追加します。

2つの登録のうちの1つに対して 'rewrite'引数をfalseに設定し、必要な書き換え規則がない場合にrewrite_rules_topに別のコードを追加するのが最善の方法です。

1
prettyboymp