Last active
August 29, 2015 14:08
-
-
Save willybahuaud/8897ff6fb73b392115a4 to your computer and use it in GitHub Desktop.
Arrangement des slugs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Pour avoir une structure de slug plus logique | |
*/ | |
add_action( 'init', 'mescpts' ); | |
function mescpts() { | |
register_post_type( 'recette', array( | |
'label' => 'Recettes', | |
'public' => true, | |
'supports' => array( 'title', 'editor', 'thumbnail' ), | |
'menu_icon' => 'dashicons-book-alt', | |
'has_archive' => false, | |
'rewrite' => array( 'slug' => _x( 'degustation/%type-recette%', 'URL slug', 'domain' ),'with_front' => true ) | |
) ); | |
register_taxonomy( 'type-recette', 'recette', array( | |
'hierarchical' => true, | |
'label' => 'Type de recette', | |
'rewrite' => array( 'slug' => _x( 'degustation', 'URL slug', 'domain' ) ) | |
) ); | |
} | |
add_filter('post_type_link', 'events_permalink_structure', 999, 4); | |
function events_permalink_structure( $post_link, $post, $leavename, $sample ) | |
{ | |
if ( false !== strpos( $post_link, '%type-recette%' ) ) { | |
$event_type_term = get_the_terms( $post->ID, 'type-recette' ); | |
$post_link = str_replace( '%type-recette%', array_pop( $event_type_term )->slug, $post_link ); | |
} | |
return $post_link; | |
} | |
add_filter( 'post_type_archive_link', 'my_archive_link', 10, 2 ); | |
function my_archive_link( $link, $cpt ) { | |
if ( 'recette' == $cpt ) { | |
$link = home_url( user_trailingslashit( 'degustation', 'post_type_archive' ) ); | |
} | |
return $link; | |
} | |
add_filter( 'rewrite_rules_array', 'my_rewrite_rules' ); | |
function my_rewrite_rules( $aRules ) { | |
$aNewRules = array( _x( 'degustation', 'URL slug', 'domain' ) . '/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?type-recette=$matches[1]&paged=$matches[2]'); | |
$bNewRules = array( _x( 'degustation', 'URL slug', 'domain' ) . '/?$' => 'index.php?post_type=recette'); | |
$cNewRules = array( _x( 'degustation', 'URL slug', 'domain' ) . '/page/?([0-9]{1,})/?$' => 'index.php?post_type=recette&paged=$matches[1]'); | |
$aRules = $aNewRules + $bNewRules + $cNewRules + $aRules; | |
return $aRules; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment