Created
September 19, 2016 13:19
-
-
Save nucklearproject/e6774b0edb0d7b4905fe0c8370fad06b to your computer and use it in GitHub Desktop.
Worepress - No listar en los posts los posts que no tengan una categoria definida.
This file contains hidden or 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
//if the post is category "uncategorized" or child of "uncategorized" as the main category, change the permalink rule of "/%category%/%postname%" to "/%postname%" | |
function my_pre_post_link($permalink, $post, $leavename) | |
{ | |
if ($post->post_type != 'post') { | |
return $permalink; | |
} | |
$cats = get_the_category($post->ID); | |
if (!count($cats)) { | |
return $permalink; | |
} | |
usort($cats, '_usort_terms_by_ID'); | |
$category_object = apply_filters('post_link_category', $cats[0], $cats, $post); | |
$category_object = get_term($category_object, 'category'); | |
return _clear_uncategorized($category_object, $permalink); | |
} | |
function _clear_uncategorized($cat, $permalink) | |
{ | |
if ($cat->slug == 'sin-categoria') { | |
return str_replace('%category%/', '', $permalink); | |
} | |
$parent = $cat->parent; | |
if (!$parent) { | |
return $permalink; | |
} | |
return _clear_uncategorized($parent, $permalink); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment