Hinzufügen von Kategorien zum benutzerdefinierten Beitragstyp in Permalink
-
-
Dasmageine dumme Frage sein,aber haben Sie Ihre Umschreibungengelöscht?this may be a silly question, but have you flushed your rewrites?
- 2
- 2012-12-19
- kristina childs
-
Vor kurzem habeich dieses Problem.** Gelöst! ** [# 188834] [1] [1]: http://wordpress.stackexchange.com/questions/94817/add-category-base-to-url-in-custom-post-type-taxonomy/188834#188834Recently, I face this issue. **Solved!** [#188834][1] [1]: http://wordpress.stackexchange.com/questions/94817/add-category-base-to-url-in-custom-post-type-taxonomy/188834#188834
- 0
- 2015-05-20
- maheshwaghmare
-
3 Antworten
- Stimmen
-
- 2015-07-26
Ich habeeine LÖSUNGgefunden !!!
(Nachendloser Recherche kannich CUSTOM POST TYPE Permalinks haben wie:
example.com/category/sub_category/my-post-name
hier der Code (infunctions.php oder Plugin):
//===STEP 1 (affect only these CUSTOM POST TYPES) $GLOBALS['my_post_typesss__MLSS'] = array('my_product1','....'); //===STEP 2 (create desired PERMALINKS) add_filter('post_type_link', 'my_func88888', 6, 4 ); function my_func88888( $post_link, $post, $sdsd){ if (!empty($post->post_type) && in_array($post->post_type, $GLOBALS['my_post_typesss']) ) { $SLUGG = $post->post_name; $post_cats = get_the_category($id); if (!empty($post_cats[0])){ $target_CAT= $post_cats[0]; while(!empty($target_CAT->slug)){ $SLUGG = $target_CAT->slug .'/'.$SLUGG; if (!empty($target_CAT->parent)) {$target_CAT = get_term( $target_CAT->parent, 'category');} else {break;} } $post_link= get_option('home').'/'. urldecode($SLUGG); } } return $post_link; } // STEP 3 (by default, while accessing: "EXAMPLE.COM/category/postname" // WP thinks, that a standard post is requested. So, we are adding CUSTOM POST // TYPE into that query. add_action('pre_get_posts', 'my_func4444', 12); function my_func4444($q){ if ($q->is_main_query() && !is_admin() && $q->is_single){ $q->set( 'post_type', array_merge(array('post'), $GLOBALS['my_post_typesss'] ) ); } return $q; }
I have found a SOLUTION!!!
(After endless research.. I can have CUSTOM POST TYPE permalinks like:
example.com/category/sub_category/my-post-name
here the code (in functions.php or plugin):
//===STEP 1 (affect only these CUSTOM POST TYPES) $GLOBALS['my_post_typesss__MLSS'] = array('my_product1','....'); //===STEP 2 (create desired PERMALINKS) add_filter('post_type_link', 'my_func88888', 6, 4 ); function my_func88888( $post_link, $post, $sdsd){ if (!empty($post->post_type) && in_array($post->post_type, $GLOBALS['my_post_typesss']) ) { $SLUGG = $post->post_name; $post_cats = get_the_category($id); if (!empty($post_cats[0])){ $target_CAT= $post_cats[0]; while(!empty($target_CAT->slug)){ $SLUGG = $target_CAT->slug .'/'.$SLUGG; if (!empty($target_CAT->parent)) {$target_CAT = get_term( $target_CAT->parent, 'category');} else {break;} } $post_link= get_option('home').'/'. urldecode($SLUGG); } } return $post_link; } // STEP 3 (by default, while accessing: "EXAMPLE.COM/category/postname" // WP thinks, that a standard post is requested. So, we are adding CUSTOM POST // TYPE into that query. add_action('pre_get_posts', 'my_func4444', 12); function my_func4444($q){ if ($q->is_main_query() && !is_admin() && $q->is_single){ $q->set( 'post_type', array_merge(array('post'), $GLOBALS['my_post_typesss'] ) ); } return $q; }
-
- 2017-07-20
Habe die Lösung!
Um hierarchische Permalinksfürbenutzerdefinierte Beitragstypen zuerhalten,installieren Siebenutzerdefinierte Beitragstyp-Permalinks ( https://wordpress.org/plugins/custom-post-type-permalinks/) Plugin.
Registrierten Beitragstyp aktualisieren. Ich habe den Namen des Beitragstyps als Hilfezentrum
function help_centre_post_type(){ register_post_type('helpcentre', array( 'labels' => array( 'name' => __('Help Center'), 'singular_name' => __('Help Center'), 'all_items' => __('View Posts'), 'add_new' => __('New Post'), 'add_new_item' => __('New Help Center'), 'edit_item' => __('Edit Help Center'), 'view_item' => __('View Help Center'), 'search_items' => __('Search Help Center'), 'no_found' => __('No Help Center Post Found'), 'not_found_in_trash' => __('No Help Center Post in Trash') ), 'public' => true, 'publicly_queryable'=> true, 'show_ui' => true, 'query_var' => true, 'show_in_nav_menus' => false, 'capability_type' => 'page', 'hierarchical' => true, 'rewrite'=> [ 'slug' => 'help-center', "with_front" => false ], "cptp_permalink_structure" => "/%help_centre_category%/%post_id%-%postname%/", 'menu_position' => 21, 'supports' => array('title','editor', 'thumbnail'), 'has_archive' => true )); flush_rewrite_rules(); } add_action('init', 'help_centre_post_type');
Und hierist die registrierte Taxonomie
function themes_taxonomy() { register_taxonomy( 'help_centre_category', 'helpcentre', array( 'label' => __( 'Categories' ), 'rewrite'=> [ 'slug' => 'help-center', "with_front" => false ], "cptp_permalink_structure" => "/%help_centre_category%/", 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'query_var' => true ) ); } add_action( 'init', 'themes_taxonomy');
Mit dieser Zeilefunktioniert Ihr Permalink
"cptp_permalink_structure" => "/%help_centre_category%/%post_id%-%postname%/",
Sie können
behalten%post_id%
entfernen und/%help_centre_category%/%postname%/"
Vergessen Sienicht,Permalinks aus dem Dashboard zu löschen.
Got the solution!
To have hierarchical permalinks for custom post type install Custom Post Type Permalinks(https://wordpress.org/plugins/custom-post-type-permalinks/) plugin.
Update registered post type. I have post type name as help center
function help_centre_post_type(){ register_post_type('helpcentre', array( 'labels' => array( 'name' => __('Help Center'), 'singular_name' => __('Help Center'), 'all_items' => __('View Posts'), 'add_new' => __('New Post'), 'add_new_item' => __('New Help Center'), 'edit_item' => __('Edit Help Center'), 'view_item' => __('View Help Center'), 'search_items' => __('Search Help Center'), 'no_found' => __('No Help Center Post Found'), 'not_found_in_trash' => __('No Help Center Post in Trash') ), 'public' => true, 'publicly_queryable'=> true, 'show_ui' => true, 'query_var' => true, 'show_in_nav_menus' => false, 'capability_type' => 'page', 'hierarchical' => true, 'rewrite'=> [ 'slug' => 'help-center', "with_front" => false ], "cptp_permalink_structure" => "/%help_centre_category%/%post_id%-%postname%/", 'menu_position' => 21, 'supports' => array('title','editor', 'thumbnail'), 'has_archive' => true )); flush_rewrite_rules(); } add_action('init', 'help_centre_post_type');
And here is registered taxonomy
function themes_taxonomy() { register_taxonomy( 'help_centre_category', 'helpcentre', array( 'label' => __( 'Categories' ), 'rewrite'=> [ 'slug' => 'help-center', "with_front" => false ], "cptp_permalink_structure" => "/%help_centre_category%/", 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_admin_column' => true, 'show_in_nav_menus' => true, 'query_var' => true ) ); } add_action( 'init', 'themes_taxonomy');
This is line makes your permalink work
"cptp_permalink_structure" => "/%help_centre_category%/%post_id%-%postname%/",
you can remove
%post_id%
and can keep/%help_centre_category%/%postname%/"
Don't forget to flush permalinks from dashboard.
-
+1 Dieeinfachste Lösungbesteht darin,nur dieses Plugin zu verwenden: https://wordpress.org/plugins/custom-post-type-permalinks/funktionierteinwandfrei+1 the simplest solution is to just use this plugin: https://wordpress.org/plugins/custom-post-type-permalinks/ works perfectly
- 0
- 2017-08-16
- Jules
-
Ja,aber dasist,wenn Sieeinen einzelnenbenutzerdefinierten Beitragstyp haben,aber wenn Siemehrerebenutzerdefinierte Beitragstypenin einemeinzigen Thema haben,ist oben die Lösung. Darüber hinaus ändert sich auch Ihre Kategorie-Sluggenauso wie Ihre Post-Type-Slug.Yes, but that is for if you have single custom post type but if you have multiple custom post type in single theme then above is the solution. Moreover it also changes your category slug same as your post type slug.
- 0
- 2017-08-18
- Varsha Dhadge
-
- 2012-05-30
Sie habenmehrere Fehlermit Ihrem Code. Ich habe Ihren vorhandenen Codebereinigt:
<?php function jcj_club_post_types() { $labels = array( 'name' => __( 'Jazz Clubs' ), 'singular_name' => __( 'Jazz Club' ), 'add_new' => __( 'Add New' ), 'add_new_item' => __( 'Add New Jazz Club' ), 'edit' => __( 'Edit' ), 'edit_item' => __( 'Edit Jazz Clubs' ), 'new_item' => __( 'New Jazz Club' ), 'view' => __( 'View Jazz Club' ), 'view_item' => __( 'View Jazz Club' ), 'search_items' => __( 'Search Jazz Clubs' ), 'not_found' => __( 'No jazz clubs found' ), 'not_found_in_trash' => __( 'No jazz clubs found in Trash' ), 'parent' => __( 'Parent Jazz Club' ), ); $args = array( 'public' => true, 'show_ui' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'menu_position' => 5, 'query_var' => true, 'supports' => array( 'title','editor','comments','revisions','trackbacks','author','excerpt','thumbnail','custom-fields' ), 'rewrite' => array( 'slug' => 'jazz-clubs-in', 'with_front' => true ), 'has_archive' => true ); register_post_type( 'jcj_club', $args ); } add_action( 'init','jcj_club_post_types' ); ?>
Ersetzen Sie Ihren Code durch den obigen Code undprüfen Sie,oberfunktioniert. Antworte zurück,wenn du weitere Fragen hast undich werde versuchen zu helfen.
BEARBEITEN:
Mirist aufgefallen,dassich
'has_archive' => true
.You have several errors with your code. I cleaned up your existing code:
<?php function jcj_club_post_types() { $labels = array( 'name' => __( 'Jazz Clubs' ), 'singular_name' => __( 'Jazz Club' ), 'add_new' => __( 'Add New' ), 'add_new_item' => __( 'Add New Jazz Club' ), 'edit' => __( 'Edit' ), 'edit_item' => __( 'Edit Jazz Clubs' ), 'new_item' => __( 'New Jazz Club' ), 'view' => __( 'View Jazz Club' ), 'view_item' => __( 'View Jazz Club' ), 'search_items' => __( 'Search Jazz Clubs' ), 'not_found' => __( 'No jazz clubs found' ), 'not_found_in_trash' => __( 'No jazz clubs found in Trash' ), 'parent' => __( 'Parent Jazz Club' ), ); $args = array( 'public' => true, 'show_ui' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'menu_position' => 5, 'query_var' => true, 'supports' => array( 'title','editor','comments','revisions','trackbacks','author','excerpt','thumbnail','custom-fields' ), 'rewrite' => array( 'slug' => 'jazz-clubs-in', 'with_front' => true ), 'has_archive' => true ); register_post_type( 'jcj_club', $args ); } add_action( 'init','jcj_club_post_types' ); ?>
Replace your code with the code above and see if it works. Reply back if you have further questions and I'll try to help.
EDIT:
I noticed I left out
'has_archive' => true
.
Ich weiß,dass die Leute dies schoneinmalgefragt haben und so weitgegangen sind,denbenutzerdefinierten Beitragstyp hinzuzufügen undfür Permalinkneu zu schreiben.
Das Problemist,dassich 340 vorhandene Kategorien habe,dieich weiterhin verwendenmöchte. Früher konnteich/category/subcategory/postname
sehenJetzt habeich den Slug von customposttype/postname. Die Auswahl der Kategorie wirdim Permalinknichtmehr angezeigt ... Ich habe die Permalink-Einstellungim Administratornicht aufetwas anderesgeändert.
Fehltetwas odermussich diesem Code hinzufügen?