Question

I have a page structure like this:

-Home
-Cars
  -Volvo 640
   - Pics
   - Info
  -Porsche 911
   - Pics
   - Info

I'd like to generate a list of cars on the Cars page of all the cars (which are child pages of Cars). How would I do this? The list is basically a sub-menu which should show all the cars in alphabetical order (note, I don't need links to the 'grandchildren' - Pics, Info). I also need to grab Custom Field data from each car page, and put that beside the link to the page.. is that possible?

Was it helpful?

Solution

You could use get_pages to do this, like so:

<?php
    $args = array(
        'post_type' => 'page',
        'child_of' => 7,
        );
    $postobj = get_pages($args);
    foreach($postobj as $item){
        $dir = get_bloginfo('template_directory'); // Theme directory
        $title = $item->post_title;
        $parent = $item->post_parent;
        $id = $item->guid;
        $name = $item->post_name;

Once you get to here, you can pull out your custom fields and put them into variables.

        $model_number = get_post_meta($item->ID, 'model_number', true);

I would use an if statement of some kind to build those top headings. For instance you could do:

            if($model_number == true){
                echo stuff;
            } else {
                echo other stuff;
        }
    }
?>

It's rough, but I think this could get you quite a long ways. Essentially, you're programmatically building your headings and returning everything to get printed. The key is formatting everything and getting your conditions set up right.

OTHER TIPS

The wp_list_pages() function can give you a list of your child pages. Grabbing custom field data from each page, though, would require a separate query and a bit more work. But here's a start:

$args = array(
    depth => '1',
    child_of => '123'
);

wp_list_pages( $args );

This will give you a list of links to all of the child pages of page ID 123. The depth specification is to prevent retrieving grandchild pages as well.

Like I said before, though, getting the custom field data is a bit trickier. You'll need to first get the page ID of each of your child pages (probably using query_posts()) and store them in an array. Then you'll loop through that array and get the custom field data from each page in turn.

So it's doable ... but I can't offer a quick solution off the top of my head.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top