Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this script to get the ancestors ID

//Get root ID
$post_id = $post->ID; 
$ancestors = get_post_ancestors($post_id);

I would like to make a redirection if it's empty to a specific page ID so no one could be on a page with no ancestors. How ?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The simple answer is wp_redirect($url);, but it's a tricky beast to use as you can only use it before any page output. So basically, what you have would need to be right at the top of your page

This example is making the assumption that you are using this in single.php? -

$post_id = $post->ID; 
$ancestors = get_post_ancestors($post_id);

if(empty($ancestors)) : // Not sure how $ancestors is returned, this assumes it's an array
    wp_redirect($url);
    exit;
endif;

See the Codex of wp_redirect() for more information.

share|improve this answer
2  
wp_redirect($url); exit; –  Max Yudin Mar 15 '13 at 15:29
1  
Good point, have edited my answer. –  David Gard Mar 15 '13 at 15:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.