• Resolved lukeshumard

    (@lukeshumard)


    I am attempting to ease my way into theme development, and am trying to display a custom value (“artist_photo_thumb”) for each child page on its parent page.

    Attached to the parent page “Artists”, there are child pages which each have the custom value “artist_photo_thumb”, which is a URL to an image. When on the parent page, I want each child page to be a list item in an unordered list, with the “artist_photo_thumb” being the image that should be displayed along with the child page name below it.

    https://dev.lukeshumard.com/blackcity/artists/

    Right now I have it listed as just the child page names, and I have no idea how to query the child pages.

    <?php
    $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
       if ($children) { ?>
         <ul id="artists">
          <?php echo $children; ?>
         </ul>
    <?php } ?>

    I don’t want the code to be done for me, but any ideas as to how this might be possible would be greatly appreciated. Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • check out get_pages()
    https://codex.www.ads-software.com/Function_Reference/get_pages

    which lets you specify to pull child pages of a parent page. Returns the page data in an array. You can process the array in a loop and use get_post_meta() to grab the custom fields from each child page.

    $parentID = 232; // ID of the parent page
    $pages = get_pages("child_of=$parentID");
      foreach($pages as $child) {
        $thumbURL = get_post_meta($child->ID, 'artist_photo_thumb', true);
        {  echo child link here }
    }

    I didn’t test this code – it may need a little tweaking but it demonstrates the basic idea.

    Thread Starter lukeshumard

    (@lukeshumard)

    I got this to work! thank you so much for your help. Here is how I edited the template based on what was written to display the custom value photo, band name, and permalink.

    While it might be constantly changing, here is it in use.
    https://dev.lukeshumard.com/blackcity/artists/

    <ul id="artists">
    
    			<?php $parentID = 4;
    			$pages = get_pages("child_of=$parentID");
    			foreach($pages as $child) {
    				$thumbURL = get_post_meta($child->ID, 'artist_photo_thumb', true);
    				$bandname = get_the_title($child->ID);
                    $bandlink = get_permalink($child->ID);?>
                    <li><a href="<?php echo $bandlink ; ?>"><img src="<?php echo $thumbURL ; ?>" alt="" /><?php echo $bandname ; ?></a></li>
    				<?php } ?>
    
                </ul>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Displaying Custom Values for Child Pages on Parent Page’ is closed to new replies.