Skip to content

Instantly share code, notes, and snippets.

@proweb
Forked from neverything/redirect-to-child.php
Created December 25, 2019 14:24
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 proweb/dbb3b3cb106e2fd96603879185a5bd33 to your computer and use it in GitHub Desktop.
Save proweb/dbb3b3cb106e2fd96603879185a5bd33 to your computer and use it in GitHub Desktop.
WordPress redirection page template to the first child page (menu_order) with WPML handling
<?php
/**
* Template Name: Redirect to child page
* Description: Redirects to the top child page
*
* @package PUT YOUR THEME NAME HERE :D
*/
global $post;
$parent = $post->ID;
// In case you use WPML
if ( defined( 'ICL_LANGUAGE_CODE' ) ) {
$_type = get_post_type( $post->ID );
$parent = icl_object_id( $post->ID, $_type, true, ICL_LANGUAGE_CODE );
}
$post_children = get_children( array(
'posts_per_page' => 1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_type' => 'page',
'post_status' => 'publish',
'post_parent' => $parent
) );
wp_redirect( get_permalink( array_pop( $post_children )->ID ), 301 );
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment