Forums

[resolved] Use Custom Field = parent id to generate list of child pages of the parent? (9 posts)

  1. charmedworks
    Member
    Posted 1 year ago #

    I would like to use a custom field in this manner:

    1. Enter id number of a parent page *not the page being edited*
    2. Output on page being edited is a list of child pages of that parent page id

    Bonus would be:

    3. If the page being edited does not use the custom field, there is no output.

    For reference here is the site:
    http://www subsurfaceconstruction.com

    The site includes a list of services and then a projects section where the project types are the same as the servies, but include children pages of projects. I'd like to list project "children" pages from the projects section on the service section pages that have the same name. So the Soil Nail Wall *service* page would include a list of Soil Nail Wall *project* page children. (You will see a list on the service page now, but it's being done manually in the page editor and I'd like to automate it.)

    Has anyone tried this or something similar? Thanks in advance!

  2. charmedworks
    Member
    Posted 1 year ago #

    Example Service section page:
    http://www.subsurfaceconstruction.com/services/soil-nail-walls/
    (list at bottom being entered manually in the editor)

    Example Project section page with list of children:
    http://www.subsurfaceconstruction.com/projects-2/soil-nail-walls/

  3. vtxyzzy
    Member
    Posted 1 year ago #

    I think the code below will do what you want. Add the custom field 'Project Page ID' to your Service page and place the code in the template.

    <?php
    $project_page_id = get_post_meta($post->ID,'Project Page ID',true);
    if ($project_page_id) {
      echo '<h3>PROJECTS USING THIS SERVICE</h3>';
      wp_list_pages('title_li=&child_of=327&echo=1');
    }
    ?>

    Now, about that free retaining wall ... :)

  4. charmedworks
    Member
    Posted 1 year ago #

    Thanks I'll give that a try! For the wall... I imagine these run far more than the client is paying for the entire website ;)

  5. charmedworks
    Member
    Posted 1 year ago #

    Ok, the code provided the 3rd part of my question (and works nicely), but not so much the first two. Perhaps I can explain better using code - the code below will NOT work, but it exemplifies what I am trying to do.

    ( See where I put into wp_list_pages - ...child_of='Project Page ID'... that's where I'd like the number from the custom field to go to generate the list of projects that are children of a particular page number)

    <?php
    $project_page_id = get_post_meta($post->ID,'Project Page ID',true);
    if ($project_page_id) {
      echo '<h3>PROJECTS USING THIS SERVICE</h3>';
      wp_list_pages('title_li=&child_of='Project Page ID'&echo=1');
    }
    ?>
  6. vtxyzzy
    Member
    Posted 1 year ago #

    Sorry, I hard-coded in a 327 for my testing and forgot to take it out. Please try this:

    <?php
    $project_page_id = get_post_meta($post->ID,'Project Page ID',true);
    if ($project_page_id) {
      echo '<h3>PROJECTS USING THIS SERVICE</h3>';
      wp_list_pages("title_li=&child_of=$project_page_id&echo=1");
    }
    ?>

    Be sure to put in the double-quotes.

    EDIT: I took out a comment about walling off a foreign country.

  7. charmedworks
    Member
    Posted 1 year ago #

    Ok, I thought that 327 looked odd.

  8. charmedworks
    Member
    Posted 1 year ago #

    Hey that works great:
    http://www.subsurfaceconstruction.com/services/soil-nail-walls/

    I'm still learning about the use of custom fields so this was *very* helpful.

    Which country Germany or Spain?

  9. vtxyzzy
    Member
    Posted 1 year ago #

    I can't name the country, but it starts with 'X'.

    If your problem is resolved, please use the dropdown at top right to mark this topic 'Resolved'.

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic