0

So I am in a single page and I want to get some meta information from a parent page. This code is inside my footer.php:

if (is_single()) {
    global $post;
    $parent = get_post_achestor ( $post->ID );
    $some_value = get_post_meta( $parent, 'some_metabox_param', true);
}

It's not working people, can you please have a look?

UPDATE: the parent page in question is template that has this loop

<?php query_posts( 'post_type=post&posts_per_page=5&paged=1' ); 
if ( have_posts() ) : global $more; ?>              
<div class="items">
    <?php while ( have_posts() ) : the_post(); $more = 0; ?>
         <?php get_template_part( 'content', get_post_format() ); ?>
     <?php endwhile; ?>
     </div>
 <?php endif; ?>    
  • try to check what you get in $parent variable. echo $parent; – 0_0 May 28 '13 at 7:27
  • the echo is "array", that's all. – thednp May 28 '13 at 7:40
  • This shows array(0) { } – thednp May 28 '13 at 7:48
  • array(0){} means, this post has no meta data. make you sure you $post->ID is not empty – 0_0 May 28 '13 at 8:30
  • Never ever use query_posts. And if you have to, use reset_postdata after your loop... – kuchenundkakao Nov 4 '15 at 21:43
0

The ID of the parent post is in $post->post_parent. When a post has no parent, that number is 0. So …

if ( is_singular() ) 
{
    global $post;
    if ( 0 !== (int) $post->post_parent )
    {
        $some_value = get_post_meta( $post->post_parent, 'some_metabox_param', true );
        if ( ! empty ( $some_value ) )
            echo $some_value;
    }
}
  • Thanks but it's not working. The parent page I am trying to get the meta info from is a custom page with a loop built with query_posts(), maybe this shed some light? – thednp May 28 '13 at 9:44
  • 1
    A page (post) is an object, not a loop. – fuxia May 28 '13 at 9:45
  • I've updated the question, please check :) – thednp May 28 '13 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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