Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hawkidoki/94086c2e45f73947f6c4d74d62f30f19 to your computer and use it in GitHub Desktop.
Save hawkidoki/94086c2e45f73947f6c4d74d62f30f19 to your computer and use it in GitHub Desktop.
<?php
// Post Type Subpages: Création des Règles de Rewrite Rules
add_action('generate_rewrite_rules', 'hwk_ptsp_rewrite_rules');
function hwk_ptsp_rewrite_rules($wp_rewrite){
if(!$post_types = hwk_ptsp_get_post_types())
return;
$wp_rules = $wp_rewrite->rules;
$new_rules = array();
foreach($post_types as $post_type){
if(!$post_type_rewrite = hwk_ptsp_get_post_type_rewrite($post_type['post_type']))
continue;
foreach($post_type['pages'] as $page){
$tag = '([^/]+)';
$prepend = $append = false;
if(hwk_ptsp_page_has_rewrite($page)){
if(isset($page['rewrite']['prepend']) && !empty($page['rewrite']['prepend']))
$tag = $page['rewrite']['prepend'] . '-' . $tag;
if(isset($page['rewrite']['append']) && !empty($page['rewrite']['append']))
$tag .= '-' . $page['rewrite']['append'];
$match = '&' . $page['name'] . '=$matches[2]';
$paged = '&paged=$matches[3]';
}else{
$tag = $page['name'];
$match = '&' . $page['name'] . '=' . $page['name'];
$paged = '&paged=$matches[2]';
}
if($page['pagination'])
$new_rules[$post_type_rewrite . '/([^/]+)/' . $tag . '/page/([0-9]{1,})/?$'] = 'index.php?' . $post_type['post_type'] . '=$matches[1]' . $match . $paged;
$new_rules[$post_type_rewrite . '/([^/]+)/' . $tag . '/?$'] = 'index.php?' . $post_type['post_type'] . '=$matches[1]' . $match;
}
}
$wp_rewrite->rules = array_merge($new_rules, $wp_rules);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment