Created
July 1, 2019 13:01
-
-
Save developer-anuragsingh/75876a911522d4edec94bacbceee7e87 to your computer and use it in GitHub Desktop.
Modify post permalinks
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
<?php | |
add_action('registered_post_type', 'make_posts_hierarchical', 10, 2); | |
function make_posts_hierarchical($post_type, $pto) | |
{ | |
if ($post_type != 'post') return; // Return, if not post type posts | |
global $wp_post_types; // access $wp_post_types global variable | |
$wp_post_types['post']->hierarchical = 1; // Set post type "post" to be hierarchical | |
// add_post_type_support('post', 'page-attributes'); // Add page attributes to post backend | |
} | |
add_filter('post_link', 'add_parent_child_cat_in_post_permalink', 10, 3); | |
// add_filter('pre_post_link', 'add_parent_child_cat_in_post_permalink', 10, 3); | |
function add_parent_child_cat_in_post_permalink($permalink, $post, $leavename) | |
{ | |
$updated_permalink = ''; | |
$cat_slug = ''; | |
if ($post->post_type != 'post') { | |
return $permalink; | |
} | |
$taxonomy = 'category'; //Put your custom taxonomy term here | |
$terms = wp_get_post_terms($post->ID, $taxonomy); | |
// foreach ($terms as $term) { | |
// if ($term->parent == 0) // this gets the parent of the current post taxonomy | |
// { | |
// $parent_term = $term; | |
// } | |
// } | |
// echo '' . $parent_term->name . ''; | |
// $cat_slug = $parent_term->name; | |
// Right, the parent is set, now let's get the children | |
foreach ($terms as $term) { | |
if ($term->parent != 0) // this ignores the parent of the current post taxonomy | |
{ | |
$child_term = $term; // this gets the children of the current post taxonomy | |
$cat_slug = $child_term->name . '/'; | |
} | |
} | |
$updated_permalink = str_replace($post->post_name, $cat_slug . $post->post_name, $permalink); | |
return $updated_permalink; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment