Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Parent page custom fields WordPress

  • SOLVED

I need to display a custom-field value of page when I'm in a subpage too.
If you look at my code you can see a div id="desc".


<?php $header_image = get_header_image();
if ( ! empty( $header_image ) && !is_page('Home') ) { ?>
<div id="banner" class="clearfix">
<div class="banner-img">
<img src="<?php header_image(); ?>" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" />
</div>
<div id="desc">
<div class="page-title"><?php wp_title(''); ?></div>

<?php the_meta(); ?>
<?php $btext = get_post_meta($post->post_parent, 'banner-text', true);
echo $btext;?>
</div>

<?php } // if ( ! empty( $header_image ) ) ?>




After display the page-title I need to check if I'm in page, I think something like:

if is a page then <?php the_meta(); ?>
else if is a subpage <?php $btext = get_post_meta($post->post_parent, 'banner-text', true); echo $btext;?> to display the parent custom-filed value.


What I need is to have in output the same structure in both cases so I can manipulate it via css only with a definition.

In the first case infact I'll have:
<div id="desc">
<div class="page-title"> Title</div>
<ul class="post-meta">
<li>
<span class="post-meta-key">banner-text:</span>
Lorem ipsum.... </li>
</ul>
</div>


but in the second:
<div id="desc">
<div class="page-title"> Title</div>
<ul class="post-meta"> </ul>
</div>
Lorem ipsum...

Answers (4)

2012-09-25

Arnav Joy answers:

try this

<?php $header_image = get_header_image();

if ( ! empty( $header_image ) && !is_page('Home') ) { ?>

<div id="banner" class="clearfix">

<div class="banner-img">

<img src="<?php header_image(); ?>" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" />

</div>

<div id="desc">

<div class="page-title"><?php wp_title(''); ?></div>


<?php
if($post->post_parent == 0 )
the_meta();
else{
$btext = get_post_meta($post->post_parent, 'banner-text', true);

echo $btext;
}
?>

</div>



<?php } // if ( ! empty( $header_image ) ) ?>


Arnav Joy comments:

try using this code

<?php $header_image = get_header_image();



if ( ! empty( $header_image ) && !is_page('Home') ) { ?>



<div id="banner" class="clearfix">



<div class="banner-img">



<img src="<?php header_image(); ?>" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" />



</div>



<div id="desc">



<div class="page-title"><?php wp_title(''); ?></div>





<?php

if($post->post_parent == 0 )

the_meta();

else{

$btext = get_post_meta($post->post_parent, 'banner-text', true);

echo '<ul class="post-meta">

<li><span class="post-meta-key">banner-text:</span>'.$btext.'</li>

</ul>';

}

?>



</div>







<?php } // if ( ! empty( $header_image ) ) ?>


natspace comments:

Thank you!

2012-09-25

John Cotton answers:

Do you mean like this?


<?php $header_image = get_header_image();

if ( ! empty( $header_image ) && !is_page('Home') ) { ?>

<div id="banner" class="clearfix">

<div class="banner-img">

<img src="<?php header_image(); ?>" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" />

</div>

<div id="desc">

<div class="page-title"><?php wp_title(''); ?></div>

<?php if($post->post_parent ) {
$btext = get_post_meta($post->post_parent, 'banner-text', true);
?>

<ul class="post-meta">
<li><span class='post-meta-key'>banner-text:</span><?php echo $btext; ?></li>
</ul>

<?php } else {
the_meta();
} ?>
</div>

<?php } // if ( ! empty( $header_image ) ) ?>


natspace comments:

Thank you!!

2012-09-25

Luis Abarca answers:


Add this to your subpage

<?php
if ($post->post_parent) {
$parent = get_post($post->post_parent);
}
?>

<div id="desc">

<div class="page-title"> Title</div>

<ul class="post-meta">
<?php $btext = get_post_meta($parent->ID, 'banner-text', true); echo $btext; ?>
</ul>

</div>

Lorem ipsum...

2012-09-25

Martin Pham answers:

please try this

<?php $header_image = get_header_image();

if ( ! empty( $header_image ) && !is_page('Home') ) { ?>
<div id="banner" class="clearfix">

<div class="banner-img">

<img src="<?php header_image(); ?>" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" />

</div>

<div id="desc">

<div class="page-title"><?php wp_title(''); ?></div>



<?php if ( is_page() && $post->post_parent ) {
the_meta();
else {
echo get_post_meta($post->post_parent, 'banner-text', true)
} ?>

</div>

<?php } // if ( ! empty( $header_image ) ) ?>