Forums

[resolved] WordPress Custom Fields (7 posts)

  1. SollyNZ
    Member
    Posted 2 years ago #

    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>
  2. MichaelH
    Volunteer
    Posted 2 years ago #

    When viewing the parent page, wouldn't $post->post_parent
    be zero (0) so you'd need to use $post->ID?

  3. SollyNZ
    Member
    Posted 2 years ago #

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

  4. MichaelH
    Volunteer
    Posted 2 years ago #

    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);
    
    ?>
  5. SollyNZ
    Member
    Posted 2 years ago #

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

  6. MichaelH
    Volunteer
    Posted 2 years ago #

    <?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);
    
    ?>
  7. SollyNZ
    Member
    Posted 2 years ago #

    You are a legend!

    Thank you so much!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.