Redirecting a child term to the first page below it in Wordpress
Can the following be done in functions.php file?
I have the following url structure:
hxxp://domain.com/custom-post-type-slug/parent-term-slug/child-term-slug/page-slug/
There are no pages directly below the parent term, only child terms.
Is there a way to redirect the "root" of a child term to the first message belonging to the same child term?
hxxp://domain.com/custom-post-type-slug/parent-term-slug/child-term-slug/
need to redirect to:
hxxp://domain.com/custom-post-type-slug/parent-term-slug/child-term-slug/page-slug/
+3
source to share
1 answer
Ok, my friend, what do you want, maybe ... a rather strange request, but possible.
add_action('wp', 'get_first_child');
function get_first_child() {
global $wp_query;
if($wp_query->queried_object_id){
$args = array(
'post_parent' => $wp_query->queried_object_id,
'numberposts' => -1,
'order'=> 'ASC',
'post_status' => 'published'
);
$post = get_children($args);
//here I test if there is more than one child.. if yes I stop here if you want it to keep going just remove this
$post = (count($post) > 1) ? null : reset($post);
if($post->guid){
wp_redirect( $post->guid, 301 );
exit;
}
}
}
So what am I doing here?
All I do is bind all children to the main message you are, and reset to the first one on the line if there is more than one child.
Then I just get its guid and redirect it with 301.
TadΓ£ ... the magic was done!
Hope this is what you wanted :)
+2
source to share