• Resolved ibu

    (@ibu)


    Page(s) in question: https://www.mfhc.com/wp/our-people/partners/michael-t-mcconnell/

    Background info: I’m creating a custom template for one of my clients and am not very familiar with PHP. Anyway, a section of their site is comprised of profiles of various lawyers. All of the profiles use a common template page and custom fields fill in all of the variable data (picture, fax number, vcard etc.).

    Next to the biographic information is a sub menu consisting of child pages (Areas of practice, Education etc.). The problem I’m running into is that I don’t know how to display the same wp_list_pages output on the children and grandchildren pages.

    Example: Michael T. Mcconnell is a child page of “Our People.” The submenu visible on his profile page is the menu in question. If you click on any of the times, you’ll notice that the menu items disappear. I’m sure this is due to my unfamiliarity with PHP, and if someone could help shine some light on my grey areas, that would be very much appreciated.

    Exampe Page Hierarchy:

    • Our People
    • Michael McConnell
    • Areas of Practice
    • Education
    • Awards
    • Etc

    Submenu code currently being used:

    <?php
      $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
      if ($children) { ?>
      <ul>
      <?php echo $children; ?>
      </ul>
    <?php } ?>
Viewing 15 replies - 1 through 15 (of 15 total)
  • Thread Starter ibu

    (@ibu)

    I lied, The Example Page Hierarchy is this:

    • Our People
    • Partners
    • Michael McConnell
    • Areas of Practice
    • Education
    • Awards
    • Etc
    Thread Starter ibu

    (@ibu)

    I should also mention that I’ve read the wp_list_pages codex page and tried to work with the examples to no avail. If the answer is there I missed it due to a low understanding of php and not for a lack of effort, haha.

    Thread Starter ibu

    (@ibu)

    Quick bump in case anyone has time to reply today.

    Thread Starter ibu

    (@ibu)

    I thought so too but that doesn’t work because it displays the sub pages of the parent. Because of the page hierarchy, the pages are both parents, so you get a top level menu when looking at a person’s profile. When you click on the sub, sub menu items, the correct information is displayed but if you are on the top level profile page for someone, it’s sub pages are displayed.

    Thread Starter ibu

    (@ibu)

    Wow that made absolutely no sense.

    Both of the pages are CHILDREN is what I meant, so they display the information of their parent.

    Thread Starter ibu

    (@ibu)

    I think I need to test for something other than post_parent.

    I could do something like:

    <?php
      if($post->is_page('42,23,44,11'))
      $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
      if ($children) { ?>
      <ul>
      <?php echo $children; ?>
      </ul>
     <?php } ?

    But I’d like to keep things dynamic so if they have to add a page later they won’t have to have us do it for them. Am I on the wrong track?

    Using that example I pointed at–could you do something like…

    $partner_id = 45; // or whatever
    if($post->post_parent && $post->parent != $partner_id)

    Thread Starter ibu

    (@ibu)

    MichaelH,

    First off, thank you for your help. Unfortunately the output on the partner profile pages is the same as the example code from that you linked to. It’s possible I’ve made a mistake, so here’s the code I’m using:

    <?php
    $partner_id = 26; // "partner" page id = 26
    if($post->post_parent && $post->parent != $partner_id)
      $children = wp_list_pages("&title_li=&child_of=".$post->post_parent."&echo=0");
      else
      $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
      if ($children) { ?>
      <ul>
      <?php echo $children; ?>
      </ul>
    <?php } ?>

    And here are example pages: Cindy’s top level profile page Cindy’s admissions page.

    These are great tips and I think might actually help with the problem I posted earlier about almost the same thing – except I am trying to list parent and children of the category in the sidebar on archive.php. I’ll try these out and see if it helps me as well.

    ibu – okay not sure this is exactly it…

    <?php
    $depth = 1;
    $node = (int) $post->ID;
    $depth = $wpdb->get_var("SELECT COUNT(*) FROM (SELECT ID, @r := (SELECT post_parent FROM $wpdb->posts WHERE ID = @r AND ID <> 0) AS _parent FROM (SELECT @r := $node) vars, $wpdb->posts WHERE @r IS NOT NULL) q WHERE _parent IS NOT NULL");
      if ($depth == 4 && $post->post_parent)
      $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
      else
      $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    
      if ($children) { ?>
      <ul>
      <?php echo $children; ?>
      </ul>
      <?php } ?>
    Thread Starter ibu

    (@ibu)

    Haha Michael I stumbled upon a solution as soon as you posted yours! I’m going to test yours as well to see if we both are viable solutions in case someone else can benefit.

    Thread Starter ibu

    (@ibu)

    MichaelH yours works swimmingly.

    Here is the fix I came up with based on this post:

    <?php global $post; $thispage = $post->ID; // grabs the current post id from global and then assigns it to thispage ?>
            <?php $pagekids = get_pages("child_of=".$thispage."&sort_column=menu_order"); // gets a list of page that are sub pages of the current page and assigns then to pagekids ?>
            <?php if ($pagekids) { // if there are any values stored in pagekids and therefore the current page has subpages ?>
            	<ul>
            		<?php wp_list_pages("depth=1&title_li=&sort_column=menu_order&child_of=".$thispage); // display the sub pages of the current page only ?>
                </ul>
            <?php } else { // if there are no sub pages for the current page ?>
            	<ul>
            		<?php echo wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0'); ?>
                </ul>
    
            <?php } ?>
    Thread Starter ibu

    (@ibu)

    Thank you so much for your help MichaelH. I hope our discourse here can help other people in the future.

    Thank YOU ibu for posting your findings. Insurmountably helpful.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Displaying child pages using wp_list_pages and php’ is closed to new replies.