最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

menus - How to generate a list of child pages, and use some of their custom fields?

programmeradmin7浏览0评论

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?

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?

Share Improve this question edited Sep 2, 2010 at 12:09 nobody 2,4662 gold badges27 silver badges32 bronze badges asked Sep 2, 2010 at 12:01 cannyboycannyboy 1,0431 gold badge15 silver badges20 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

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.

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.

发布评论

评论列表(0)

  1. 暂无评论