3

4 つのカスタム投稿タイプがあります。

サービス よくある質問 前後の料金

これらの各投稿タイプ内で同じ投稿名を使用できるようにしたいです。例:

サービス
スキン (example.com/Services/skin)
クリーム (example.com/Services/cream)
ボディ (example.com/Services/body)

よくある質問
スキン (example.com/FAQs/skin)
クリーム (example.com/FAQs/cream)
ボディ (example.com/FAQs/body)

プライス
スキン (example.com/Prices/skin)
クリーム (example.com/Prices/cream)
ボディ (example.com/Prices/body)


スキンの前後(example.com/before-and-after/skin)
クリーム (example.com/before-and-after/cream)
ボディ (example.com/before-and-after/body)

これどうやってするの?現在、現在の投稿と同じ名前の新しい投稿を作成すると、投稿のスラッグの末尾に「-2」または「-3」が追加されます。

悪い:

example.com/services/body

example.com/faqs/body-2

example.com/prices/body-3

example.com/before-and-after/body-4

誰か助けてください!!!!!

add_action('init', 'create_post_type_html5'); // Add our HTML5 Blank Custom Post Type
function create_post_type_html5()
 {
  register_taxonomy_for_object_type('category', 'html5-blank'); // Register Taxonomies for Category
register_taxonomy_for_object_type('post_tag', 'html5-blank');
register_post_type('html5-blank', // Register Custom Post Type
    array(
    'labels' => array(
        'name' => __('Services', 'html5blank'), // Rename these to suit
        'singular_name' => __('Services', 'html5blank'),
        'add_new' => __('Add New', 'html5blank'),
        'add_new_item' => __('Add New Services', 'html5blank'),
        'edit' => __('Edit', 'html5blank'),
        'edit_item' => __('Edit Services', 'html5blank'),
        'new_item' => __('New Services', 'html5blank'),
        'view' => __('View Services', 'html5blank'),
        'view_item' => __('View Services', 'html5blank'),
        'search_items' => __('Search Services', 'html5blank'),
        'not_found' => __('No Servicess found', 'html5blank'),
        'not_found_in_trash' => __('No Service\'s found in Trash', 'html5blank')
    ),
    'rewrite' => array('slug' => 'service','with_front' => true),
    'public' => true,
    'hierarchical' => true, // Allows your posts to behave like Hierarchy Pages
    'has_archive' => true,
    'supports' => array(
        'title',
        'editor',
        'excerpt',
        'thumbnail'
    ), // Go to Dashboard Custom HTML5 Blank post for supports
    'can_export' => true, // Allows export in Tools > Export
    'taxonomies' => array(
        'post_tag',
        'category'
    ) // Add Category and Post Tags support
));


 }
4

6 に答える 6

7

テーマの functions.php ファイルに追加できる関数を書きました。これには、mboynes によって次のチケットに書かれたパッチが含まれています: http://core.trac.wordpress.org/ticket/18962#comment:14

function wp_cpt_unique_post_slug($slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug) {
    if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
        return $slug;

    global $wpdb, $wp_rewrite;

    // store slug made by original function
    $wp_slug = $slug;

    // reset slug to original slug
    $slug = $original_slug;

    $feeds = $wp_rewrite->feeds;
    if ( ! is_array( $feeds ) )
        $feeds = array();

    $hierarchical_post_types = get_post_types( array('hierarchical' => true) );
    if ( 'attachment' == $post_type ) {
        // Attachment slugs must be unique across all types.
        $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );

        if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) {
            $suffix = 2;
            do {
                $alt_post_name = substr ($slug, 0, (200 - ( strlen( $suffix ) + 1 )) ) . "-$suffix";
                $post_name_check = $wpdb->get_var( $wpdb->prepare($check_sql, $alt_post_name, $post_ID ) );
                $suffix++;
            } while ( $post_name_check );
            $slug = $alt_post_name;
        }
    } elseif ( in_array( $post_type, $hierarchical_post_types ) ) {
        if ( 'nav_menu_item' == $post_type )
            return $slug;
        // Page slugs must be unique within their own trees. Pages are in a separate
        // namespace than posts so page slugs are allowed to overlap post slugs.
        $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1";
        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );

        if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {
            $suffix = 2;
            do {
                $alt_post_name = substr( $slug, 0, (200 - ( strlen( $suffix ) + 1 )) ) . "-$suffix";
                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) );
                $suffix++;
            } while ( $post_name_check );
            $slug = $alt_post_name;
        }
    } else {
        // Post slugs must be unique across all posts.
        $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
        $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );

        if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
            $suffix = 2;
            do {
                $alt_post_name = substr( $slug, 0, (200 - ( strlen( $suffix ) + 1 )) ) . "-$suffix";
                $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) );
                $suffix++;
            } while ( $post_name_check );
            $slug = $alt_post_name;
        }
    }

    return $slug;
}
add_filter('wp_unique_post_slug', 'wp_cpt_unique_post_slug', 10, 6);
于 2013-07-08T17:21:28.853 に答える
2

これは wordpress プログラマー チームによってまだ解決されていないバグだと思います。これについての他の説明は次のとおりです。異なるコンテンツに対して重複したスラッグを許可する

この関数に間違ったコードが書かれているのを見つけました: コア ファイル (wp_includes/post.php) にある wp_unique_post_slug は、挿入されたパーマリンクが一意かどうかをチェックします。

残念ながら、バグを修正するためにコア コードをハックする必要がありました。2859 行目から始まるこのコード部分を置き換えました。

} elseif ( in_array( $post_type, $hierarchical_post_types ) ) {
    // Page slugs must be unique within their own trees. Pages are in a separate
    // namespace than posts so page slugs are allowed to overlap post slugs.
    $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '" . implode( "', '", esc_sql( $hierarchical_post_types ) ) . "' ) AND ID != %d AND post_parent = %d LIMIT 1";
    $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) );

    if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug )  || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {
        $suffix = 2;
        do {
            $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
            $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID, $post_parent ) );
            $suffix++;
        } while ( $post_name_check );
        $slug = $alt_post_name;
    }
}

私が書いたこのコードで:

} elseif ( in_array( $post_type, $hierarchical_post_types ) ) {

    // HACK
    foreach($hierarchical_post_types as $pt){

            // Page slugs must be unique within their own trees. Pages are in a separate
            // namespace than posts so page slugs are allowed to overlap post slugs.                                

            // HACK
            $check_sql_andrea = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d AND post_parent = %d LIMIT 1";
            // HACK
            $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql_andrea, $slug, $pt, $post_ID, $post_parent ) );

            if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug )  || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) {
                $suffix = 2;
                do {
                    $alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
                    // HACK
                    $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql_andrea, $alt_post_name, $pt, $post_ID, $post_parent ) );
                    $suffix++;
                } while ( $post_name_check );
                $slug = $alt_post_name; 
                break;                          
            }
    }// END HACK

}

お願いします... このケースの他の解決策を誰かが知っている場合、またはさらに重要なことに、私が見つけた解決策に影響を与える予測不可能な結果を​​誰かが知っている場合は、お知らせください。本当に感謝しています!

于 2012-12-03T16:04:53.607 に答える
0

送信した 2 つの質問に異なるコードを投稿しないでください。

于 2012-11-24T22:17:43.837 に答える
0

rewrite1 つの解決策は、ルールwith_frontを falseに変更することです。たとえば、次のように設定した場合

'rewrite => array( 
    'slug' => 'service',
    'with_front' => 'false'
)

競合することなく、それぞれで同じカテゴリ/分類法を使用できるはずです。

行っている内容によっては、カスタム投稿タイプごとに独自の分類法を定義する必要がある場合もあります。

于 2012-11-24T15:33:59.260 に答える
0

この問題への他の参照を検索しました (これは、異なる親カテゴリのサブカテゴリでも発生します)。さらに、将来のリリースの拡張機能としてマークされています。そのチケットで、wp -includes/post. php。多分あなたはそれを見て、現在のWPの誤動作を修正する方法を見つけるべきです.

汚い解決策は、完全なスラッグ出力を再チェックすることであり、重複がない場合は、追加された数値サフィックスを削除しますが、私はWPソースコードを読んでこれについてより多くのアドバイスを提供する初心者です.

于 2012-11-24T03:08:49.327 に答える
0

解決策は 1 つだけです。投稿には一意の識別子 (スラッグ) が必要であり、URL に同じ名前を付けることはできません。それらが階層的である場合を除きます。そして、カスタム投稿タイプでそれを行うことができます. 使用する :

        register_post_type(
            'customposttype', 
            array(
                'label' => '...',
                'description' => '...',
                'public' => true,
                'exclude_from_search' => false,
                'hierarchical' => true, ...

そうすれば、異なるカスタム投稿タイプで同じ名前を持つことができます

于 2012-11-23T21:16:53.990 に答える