Last active
January 16, 2017 09:35
-
-
Save agustinprosperi/9f3c41fd95be1fc1cc43155755fd1d8c to your computer and use it in GitHub Desktop.
Custom add_rewrite_rule y add_rewrite_tag para url personalizadas
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 | |
// custom add_rewrite_rule y add_rewrite_tag | |
add_action('init', 'custom_rewrite_rule'); | |
function custom_rewrite_rule() { | |
$rp = getFirstPostByTemplate('template-revistas.php'); | |
if( empty($rp) ) return false; | |
$param1 = 'categoria'; | |
$param2 = 'entrada'; | |
add_rewrite_tag('%'.$param1.'%', '([^&]+)'); | |
add_rewrite_tag('%'.$param2.'%', '([^&]+)'); | |
add_rewrite_rule('^'.$rp->post_name.'/([^/]*)/([^/]*)/?','index.php?page_id='.$rp->ID.'&'.$param1.'=$matches[1]&'.$param2.'=$matches[2]','top'); | |
add_rewrite_rule('^'.$rp->post_name.'/([^/]*)/?','index.php?page_id='.$rp->ID.'&'.$param1.'=$matches[1]','top'); | |
// flush_rewrite_rules es una funcion muy pesada solo debe ser llamada cuando sea estrictamente necesaria, en este caso si cambia el nombre de la pagina | |
// delete_option('revista_rewrite'); | |
$concat = $rp->post_name.$rp->ID.$param1.$param2; | |
if( get_option('revista_rewrite') != $concat ){ | |
update_option('revista_rewrite', $concat, false); | |
flush_rewrite_rules(); | |
// add_action("shutdown", "flush_rewrite_rules"); | |
} | |
} | |
// ejemplo de uso en front | |
// global $wp_query; | |
// echo 'categoria: ' . $wp_query->query_vars['categoria']; | |
// echo '<br />'; | |
// echo 'entrada: ' . $wp_query->query_vars['entrada']; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment