• Resolved sollynz

    (@sollynz)


    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)
  • When viewing the parent page, wouldn’t $post->post_parent
    be zero (0) so you’d need to use $post->ID?

    Thread Starter sollynz

    (@sollynz)

    That will display it if im on the parent page, but not on the child pages then. I need it to display on both.

    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

    (@sollynz)

    hmmm that seems to just give me a blank white page now ?

    <?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

    (@sollynz)

    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.