WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get a custom field value from a parent page to show on a child page (3 posts)

  1. belalugosi
    Member
    Posted 4 years ago #

    Hello,

    I'm using custom fields as descriptions for a site's sections: pages with children pages. The top pages have all the same custom field key, each one with different values.

    Example:
    top page 1 (custom field key: color; custom field value: blue;)
    - child page 1
    - child page 2

    top page 2 (custom field key: color; custom field value: yellow;)
    - child page 3
    - child page 4

    What I would like to accomplish is this: on each child page I would like to display the value of the custom field from the parent page. "Child page 1" and "Child page 2" have somewhere on the page "blue" and
    "Child page 3" and "Child page4" have somewhere on the page "yellow"

    Is that possible?

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    I've not tried it but something like:

    <?php $color = get_post_meta($post->post_parent, 'color', true);
    echo $color;?>

    should work if it's used within the Loop.

  3. belalugosi
    Member
    Posted 4 years ago #

    Thank you esmi. That works like a charm.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.