How do I call information from a sub page of a specific parent page in wordpress?

Ladies & Gentlemen, boys & Girls, Guru’s, Geeks and Geniuses of all ages.

I am having a problem on my wordpress theme.

Read More

I am creating a website for a company which has an online shop. The hierarchy of the shop pages is as follows:

Shop
-Catagory Page
–Product Page

One of the category pages is called “Designs” which has a page ID of 22. What I would like to do in the sidebar <aside> of wordpress is display, as a link, the title and an image from the newest sub-page of the ‘design’ page. Each product page has an image defined using a custom field called “Product_Image”.

I have found this code on stackoverflow which I’ve modified to display the data I need, but instead of just calling the latest sub-page of the design page it calls the latest page from the whole site.

<?php
   $args=array(
   'showposts'=>1,
   '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(); ?>
    <div>
      <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>
        <?php  echo "<img src='" . get_post_meta($post->ID, "Product_Image", true) . "' />"; ?>
      </a>
    </div>
  <?php
  endwhile;
}
?>

I am completely new to PHP. I have only just started learning using “Tutsplus – PHP Essentials”.

Could anyone help me modify this code so it only displays the information from the latest sub-page of the Design Page.

Related posts

Leave a Reply

1 comment

  1. You can add a new argument to your query to only get pages that are a subpage of the design page. So, modify your $args array to look like this:

    $args=array(
      'showposts'=>1,
      'post_type' => 'page',
      'caller_get_posts'=>1,
      'post_parent' => '22'  // get pages that are children of the design page
    );
    

    Leave the rest of your code as is, and it should work.
    For future reference, see here for all of the WP_Query parameters: http://codex.wordpress.org/Function_Reference/WP_Query#Parameters