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; ?>    
6
  • try to check what you get in $parent variable. echo $parent;
    – Core
    May 28, 2013 at 7:27
  • the echo is "array", that's all.
    – thednp
    May 28, 2013 at 7:40
  • This shows array(0) { }
    – thednp
    May 28, 2013 at 7:48
  • array(0){} means, this post has no meta data. make you sure you $post->ID is not empty
    – Core
    May 28, 2013 at 8:30
  • Never ever use query_posts. And if you have to, use reset_postdata after your loop... Nov 4, 2015 at 21:43

1 Answer 1

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;
    }
}
3
  • 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, 2013 at 9:44
  • 1
    A page (post) is an object, not a loop.
    – fuxia
    May 28, 2013 at 9:45
  • I've updated the question, please check :)
    – thednp
    May 28, 2013 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.