Title: WordPress Custom Fields
Last modified: August 19, 2016

---

# WordPress Custom Fields

 *  Resolved [sollynz](https://wordpress.org/support/users/sollynz/)
 * (@sollynz)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/wordpress-custom-fields/)
 * Hi,
 * Was wondering if someone could help me.
 * I have a template page that a has a side bar navigation there are going to be
   multiple pages which use this template. There is also a child pages for each 
   of these pages.
 * At the top of the sidebar i have some custom fields which are pulled from the
   parent page. These are working and displaying fine on all the child pages how
   ever when viewing the parent page the custom fields do not.
 * Here is my code
 *     ```
       <?php $parent = $post->post_parent;
       $parent_title = get_the_title($parent);
   
       $address = $post->post_parent;
       $address = get_post_meta($address, 'address', true);
       $phone = $post->post_parent;
       $phone = get_post_meta($phone, 'phone', true);
   
        ?>
           	<h2>ABC <?php echo($parent_title); ?></h2>
               <?php echo $address ?>
       		<?php echo $phone ?>
               <?php
       	$current_page = $post->ID;
       	$parent = 1;
   
       	while($parent) {
       		$page_query = $wpdb->get_row("SELECT post_name, post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
       		$parent = $current_page = $page_query->post_parent;
       		if(!$parent) $parent_name = $page_query->post_name;
       	}
       ?>
   
               <ul>
                   <li><a href="/<?php echo $parent_name ?>/our-philosophy-and-values">Our Philosophy & Values</a></li>
                   <li><a href="/<?php echo $parent_name ?>/curriculum">Curriculum</a></li>
                   <li><a href="/<?php echo $parent_name ?>/our-centre-director">Our Centre Director</a></li>
                   <li><a href="/<?php echo $parent_name ?>/photo-gallery">Photo Gallery</a></li>
                   <li><a href="/<?php echo $parent_name ?>/our-ero-reports">Our ERO Reports</a></li>
                   <li><a href="/<?php echo $parent_name ?>/policies">Policies</a></li>
                   <li><a href="/<?php echo $parent_name ?>/fees">Fees</a></li>
                   <li><a href="/<?php echo $parent_name ?>/your-first-day">Your First Day</a></li>
                   <li><a href="/<?php echo $parent_name ?>/transitioning">Transitioning</a></li>
               </ul>
       ```
   

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/wordpress-custom-fields/#post-1373024)
 * When viewing the parent page, wouldn’t `$post->post_parent`
    be zero (0) so you’d
   need to use `$post->ID`?
 *  Thread Starter [sollynz](https://wordpress.org/support/users/sollynz/)
 * (@sollynz)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/wordpress-custom-fields/#post-1373028)
 * That will display it if im on the parent page, but not on the child pages then.
   I need it to display on both.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/wordpress-custom-fields/#post-1373033)
 * Might need to change:
 *     ```
       <?php $parent = $post->post_parent;
       $parent_title = get_the_title($parent);
   
       $address = $post->post_parent;
       $address = get_post_meta($address, 'address', true);
       $phone = $post->post_parent;
       $phone = get_post_meta($phone, 'phone', true);
   
        ?>
       ```
   
 * to something like:
 *     ```
       <?php
       $id_to_get = $post-ID:
       if ( $post->post_parent ) {
         $id_to_get = $post->post_parent
       }
       $parent = $id_to_get;
       $parent_title = get_the_title($parent);
   
       $address = get_post_meta($id_to_get, 'address', true);
       $phone = get_post_meta($id_to_get, 'phone', true);
   
       ?>
       ```
   
 *  Thread Starter [sollynz](https://wordpress.org/support/users/sollynz/)
 * (@sollynz)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/wordpress-custom-fields/#post-1373035)
 * hmmm that seems to just give me a blank white page now ?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/wordpress-custom-fields/#post-1373043)
 *     ```
       <?php
       $id_to_get = $post->ID;
       if ( $post->post_parent ) {
         $id_to_get = $post->post_parent;
       }
       $parent = $id_to_get;
       $parent_title = get_the_title($parent);
   
       $address = get_post_meta($id_to_get, 'address', true);
       $phone = get_post_meta($id_to_get, 'phone', true);
   
       ?>
       ```
   
 *  Thread Starter [sollynz](https://wordpress.org/support/users/sollynz/)
 * (@sollynz)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/wordpress-custom-fields/#post-1373045)
 * You are a legend!
 * Thank you so much!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘WordPress Custom Fields’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [sollynz](https://wordpress.org/support/users/sollynz/)
 * Last activity: [16 years, 4 months ago](https://wordpress.org/support/topic/wordpress-custom-fields/#post-1373045)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
