web-dev-qa-db-ja.com

カスタム投稿タイプ、分類法、およびパーマリンク

これは私をナッツに駆り立てています、そして私はそれが単純であると確信しています、しかし私が捜す何も単純な構造を思い付きません(すべては非常に複雑です)。

私はカスタム投稿タイプproduct_listingとカスタム分類法product_catを持っています(これは階層的でカテゴリのようになるはずです)。

私のURLをこのように見せたいだけです。

mysite.com/products/category1/product-name1 
mysite.com/products/category2/product-name2

しかし、私の人生のために、私が何をしていようとも、私は恐ろしい404号の問題を抱えています。ページは問題なく動作し、投稿は問題なく動作しますが、私のカスタム投稿は正しく動作しません。彼らは次のように現れています:

mysite.com/products/product-name1
mysite.com/products/product-name2

これは実際に は動作します !カスタム分類法をそこに表示したいだけでなく、次のようにして設定したtaxonomy.phpテンプレートにアクセスできるようにしたいのです。

mysite.com/products/category1/
mysite.com/products/category2/

私のナメクジはどれも同じではありません、そして私もそれらが欲しくありません。これが私のfunctions.phpファイルの投稿タイプと分類法の部分です。

///// CUSTOM POST TYPES /////

// register the new post type
register_post_type( 'product_listing', array( 
    'labels'                 => array(
        'name'               => __( 'Products' ),
        'singular_name'      => __( 'Product' ),
        'add_new'            => __( 'Add New' ),
        'add_new_item'       => __( 'Create New Product' ),
        'edit'               => __( 'Edit' ),
        'edit_item'          => __( 'Edit Product' ),
        'new_item'           => __( 'New Product' ),
        'view'               => __( 'View Products' ),
        'view_item'          => __( 'View Product' ),
        'search_items'       => __( 'Search Products' ),
        'not_found'          => __( 'No products found' ),
        'not_found_in_trash' => __( 'No products found in trash' ),
        'parent'             => __( 'Parent Product' ),
    ),
    'description'           => __( 'This is where you can create new products on your site.' ),
    'public'                => true,
    'show_ui'               => true,
    'capability_type'       => 'post',
    'publicly_queryable'    => true,
    'exclude_from_search'   => false,
    'menu_position'         => 2,
    'menu_icon'             => get_stylesheet_directory_uri() . '/images/tag_orange.png',
    'hierarchical'          => true,
    '_builtin'              => false, // It's a custom post type, not built in!
    'rewrite'               => array( 'slug' => 'products', 'with_front' => true ),
    'query_var'             => true,
    'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' ),
) );


//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_product_taxonomies', 0 );
//add_action('admin_init', 'flush_rewrite_rules');

//create two taxonomies, genres and writers for the post type "book"
function create_product_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Category', 'taxonomy singular name' ),
        'search_items'      =>  __( 'Search Categories' ),
        'all_items'         => __( 'All Categories' ),
        'parent_item'       => __( 'Parent Categories' ),
        'parent_item_colon' => __( 'Parent Categories:' ),
        'edit_item'         => __( 'Edit Category' ), 
        'update_item'       => __( 'Update Category' ),
        'add_new_item'      => __( 'Add New Category' ),
        'new_item_name'     => __( 'New Category Name' ),
        'menu_name'         => __( 'Category' ),
    );  

    register_taxonomy( 'product_cat', array( 'product_listing' ), array(
        'hierarchical'  => true,
        'labels'        => $labels,
        'show_ui'       => true,
        'query_var'     => true,
        //'rewrite'     => true,
        'rewrite'       => array( 'slug' => '%category%', 'with_front' => true ),
    ) );

    // Add new taxonomy, NOT hierarchical (like tags)
    $labels = array(
        'name'                       => _x( 'Scents', 'taxonomy general name' ),
        'singular_name'              => _x( 'Scent', 'taxonomy singular name' ),
        'search_items'               =>  __( 'Search Scents' ),
        'popular_items'              => __( 'Popular Scents' ),
        'all_items'                  => __( 'All Scents' ),
        'parent_item'                => null,
        'parent_item_colon'          => null,
        'edit_item'                  => __( 'Edit Scent' ), 
        'update_item'                => __( 'Update Scent' ),
        'add_new_item'               => __( 'Add New Scent' ),
        'new_item_name'              => __( 'New Scent Name' ),
        'separate_items_with_commas' => __( 'Separate scents with commas' ),
        'add_or_remove_items'        => __( 'Add or remove scents' ),
        'choose_from_most_used'      => __( 'Choose from the most used scents' ),
        'menu_name'                  => __( 'Scents' ),
    ); 

    register_taxonomy( 'scent', 'product_listing', array(
        'hierarchical'  => false,
        'labels'        => $labels,
        'show_ui'       => true,
        'query_var'     => true,
        //'rewrite'     => array( 'slug' => 'scents' ),
    ) );
}

私はscentsの別のカスタム分類法も持っています。それは私が理想的にはある種のフレンドリーなURLを持ちたいのですが、私はこれに関してもっとオープンです。私はおそらくmysite.com/products/scentsに行くことによってすべての香りのリストにアクセスしたいですが、それらはカテゴリー特有である必要はありません。

誰かが私を手伝ってくれる?

61
RodeoRamsey

投稿タイプの引数のslugproducts/%product_cat%に、分類法の引数のslugを単なるproductsに変更してから、書き換え規則をフラッシュします。 WordPressは/products/my-product-cat/post-name/を処理するはずです。

最後に、私たちはパーマリンクを生成することでWordPressを少し手助けする必要があります(箱から出して、それはpermastructタグ%product_cat%を認識しません):

/**
 * Inject term slug into custom post type permastruct.
 * 
 * @link   http://wordpress.stackexchange.com/a/5313/1685
 * 
 * @param  string  $link
 * @param  WP_Post $post 
 * @return array
 */
function wpse_5308_post_type_link( $link, $post ) {
    if ( $post->post_type === 'product_listing' ) {
        if ( $terms = get_the_terms( $post->ID, 'product_cat' ) )
            $link = str_replace( '%product_cat%', current( $terms )->slug, $link );
    }

    return $link;
}

add_filter( 'post_type_link', 'wpse_5308_post_type_link', 10, 2 );

注目すべき1つのことは、これは単に投稿の最初の商品カテゴリーをつかむでしょう 名前順 。単一の商品に複数のカテゴリを割り当てる場合、パーマリンクでどのカテゴリを使用するかを決定する方法を簡単に変更できます。

あなたがこれをどうやって成し遂げるかを知っていれば、他の問題に取り組むことができます。

63
TheDeadMedic

ありがとう@TheDeadMechanic、あなたの答えは私を助けてくれました、しかし部分的にだけ。 @RodeoRamseyが要求したのと同じことをしたかったのですが、ネストされたカテゴリ(つまりmysite.com/products/category1/child-category-1/grandchild-category-1/product-name)を使用したため、解決策がうまくいきませんでした。

私はついにうまくいく私の質問に対する拡張された解決策を思いついた、それで他の誰かが入れ子にされたカテゴリ/サブカテゴリを必要とするならあなたは私自身の質問で 詳細な解決策 を見ることができる。それが他の人に役立つことを願っています、そして最初のステップをありがとう。

6
Jeff

Wpがそのままその構造をサポートしているかどうかはわかりませんが、そうするために独自の書き換え規則を簡単に作成できます。

ここで前の答えをチェックしてください Author url rewrite

あなたは行を変更することができます

$newrules['author/([^/]+)/songs/?$'] = 'index.php?post_type=songs&author=$matches[1]';

のように

$newrules['products/([^/]+)/([^/]+)/?$'] = 'index.php?post_type=product_listing&product_cat=$matches[1]&name=$matches[2]';

ここのproduct_cat部分は不必要かもしれません - それが必要であるかどうか私はわかりません。

あなたが好きなルールを追加することができ、それらは作り付けのものよりも優先されます。

4
Chris

カスタム投稿タイプのパーマリンクを設定する前に、うんざりしていました。カスタム投稿タイプを処理するプラグインを見つけました。使い方はとても簡単です。 http://wordpress.org/extend/plugins/custom-post-permalinks/ WPこれをコア機能として追加してください。しし座

2
user1975

実のところ、それはとても簡単です。あなたはただ1行必要です。これが私のコードです

関数create_product_taxonomies()
 {
 //新しい分類を追加し、階層的にする(カテゴリのように)
 $ labels = array(
 'name '=> _x('カテゴリ '、'分類一般名 ')、
'単数形 '=> _x('カテゴリ '、'分類単数名 ')、
' search_items '=> __ ( '検索カテゴリ')、
 'all_items' => __( 'すべてのカテゴリ')、
 'parent_item' => __( '親カテゴリ')、
 'parent_item_colon '=> __('親カテゴリ: ')、
' edit_item '=> __('カテゴリの編集 ')、
' update_item '=> __('カテゴリの更新 ')、[。 [add_new_item] => __([新しいカテゴリの追加])、
 [new_item_name] => __([新しいカテゴリ名])、[menu_name] => __([カテゴリ] ')、
); 
 
 register_taxonomy(' product_cat '、array(' product_listing ')、array(
' hierarchy '=> true、
 。] 'labels' => $ labels、
 'show_ui' => true、[ 'query_var' => true、
 'rewrite' => array( 'hierarchy' => true)、
)); 

そしてGenerateWP.comから私のレビューCPTのために生成された分類法に適用されます。私は自分のWordPressサイトでこれを使っています、 https://www.wpstarters.com

 function reviews_category_taxonomy(){
 
 $ labels = array(
 'name' => _x( 'レビューカテゴリ'、 '分類一般名'、 'reviews_category) ')、
' singular_name '=> _x('レビューカテゴリ '、'分類シノネーム名 '、' reviews_category ')、
' menu_name '=> __('レビューカテゴリ '、' reviews_category ')、
' all_items '=> __('すべてのレビューカテゴリ '、' reviews_category ')、
' parent_item '=> __('親レビューカテゴリ '、' reviews_category ')、[ 'parent_item_colon' => __( '親レビューカテゴリ:'、 'reviews_category')、
 'new_item_name' => __( '新しいレビューカテゴリ名'、 'reviews_category')、
 'add_new_item' => __( '新しいレビューカテゴリの追加'、 'reviews_category')、
 'edit_item' => __( 'レビューカテゴリの編集'、 'reviews_cat egory ')、
' update_item '=> __('レビューカテゴリの更新 '、' reviews_category ')、
' view_item '=> __('レビューカテゴリの表示 '、' reviews_category ')、 
 'separate_items_with_commas' => __( '項目をカンマで区切ります、' reviews_category ')、
' add_or_remove_items '=> __('項目を追加または削除してください '、' reviews_category ') ] [choose_from_most_used] => __([最もよく使用されるものから選択する]、[reviews_category])、
 [popular_items] => __([人気のあるレビューカテゴリ]、[reviews_category])、
 'search_items' => __( '検索項目'、 'reviews_category')、
 'not_found' => __( '見つかりません'、 'reviews_category')、
 'no_terms' => __( 'いいえレビューカテゴリ'、 'reviews_category')、
 'items_list' => __( 'レビューカテゴリリスト'、 'reviews_category')、
 'item s_list_navigation '=> __('レビューカテゴリリストナビゲーション '、' reviews_category ')、
); 
 $ args = array(
' labels '=> $ labels、[。 [階層] => true、
 'public' => true、
 'show_ui' => true、
 'show_admin_column' => true、
 ] 'show_in_nav_menus' => true、
 'show_tagcloud' => false、
 'show_in_rest' => true、
 'rewrite' => array( 'hierarchy' => true) )
); 
 register_taxonomy( 'reviews_category'、array( 'wps_reviews')、$ args); 
 
} 
 add_action( ' init '、' reviews_category_taxonomy '、0); 

必要なのは、 'rewrite' => array( 'hierarchy' => true)のようにしてください。

0
Leo Koo