treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] How do I call title (and custom field info) from the newest subpage of a defined parent

  • Hi people,

    I am having great difficulty with a wordpress site. I am trying to call information from a page that is a sub-page to parent, and I was hoping someone would be able to help.

    Basically I have built a website that as well as the usual home, about and content page includes a shop that which has 3 levels of hierarchy.

    The store. - The categories. Sub-pages of the store. -- The products. Sub-pages of the categories.

    Throughout the entire site I call a sidebar which displays the title and a snippet of the latest blog post and I've added a mailing list form. This bit I have done.

    However I also want to add a "latest design" heading. This would display, as a link, the title of the newest sub-page of a design category. As well as an image defined by a custom field labeled "Product_Image".

    The design page has a page id of 22.

    I know the code to display it is:

    <a href="<?php the_permalink(); ?>">
            <h3><?php the_title(); ?></h3>
            <?php  echo "<img src='" . get_post_meta($post->ID, "Product_Image", true) . "' />"; ?>
           </a>
    

    Unfortunately I do not know how to call from the newest sub-page of a specific parent.

    In short I want to basically say "Get me the title and custom field from the newest child-page of the Design page (ID=22)"

    As you can tell I am new to PHP and have I am only just beginning to learn PHP from this website and Lynda.com PHP essentials.

    Could anyone help please?

    Also as a side note is there a way to add a marker to a product page. I was thinking of having an "item of the week". By somehow marking a product page, perhaps via custom field, its information (title, image and a link) is then displayed on the homepage. I can then remove the marker and add it a different product page so the featured product can be changed week by week.

  • Off the top of my head, I don't think you can pull in pages based on date.

  • I found this which displays the most recently created pages. This is the original code, but I have modified the one on my site to only display the title and image.

    However I do not know how to modify it so rather than showing the most recently created pages on the whole site, it only shows the most recently created child page of my design page.

      <?php
       $args=array(
       'showposts'=>5,
       'post_type' => 'page',
       'caller_get_posts'=>1
       );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
    

    <?php the_time('m.d.y') ?> " rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>

        <?php
      endwhile;
    }
    ?>
    

    Any help would be appreciated.

  • I have now got the answer to my question. For anyone who reads this and is struggling with the same thing then just add the following code to the initial argument.

    'post_parent' => '22' 
    

    (replace 22 with the page id of the parent)

    The rest of the code stays the same. so it now becomes

       $args=array(
       'showposts'=>5,
       'post_type' => 'page',
       'caller_get_posts'=>1,
       'post_parent' => '22' //replace 22 with the page id of the parent.
       );
    

    Remember to add a , (comma) to the line before so the code knows 'post_parent' is a new argument.