如何从一个特定父页面的子页面调用信息?


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

女士们;先生们,孩子们!所有年龄段的女孩、大师、极客和天才。

我的wordpress主题有一个问题。

我正在为一家有网上商店的公司创建一个网站。商店页面的层次结构如下:


购物类别页
——产品页面

其中一个分类页面名为"设计",其页面ID为22。我想在wordpress的侧边栏<aside>中做的是显示,作为链接,标题和来自"设计"页面的最新子页面的图像。每个产品页面都有一个使用名为"Product_Image"的自定义字段定义的图像。

我在stackoverflow上发现了这个代码,我已经修改了它来显示我需要的数据,但它不是仅仅调用设计页面的最新子页面,而是调用整个网站的最新页面。

<?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;
}
?>

我是一个PHP新手。我才刚刚开始学习使用"Tutsplus - PHP Essentials"。

谁能帮我修改这个代码,所以它只显示从设计页面的最新子页面的信息。

您可以在查询中添加一个新参数,以便仅获取属于设计页面的子页面的页面。因此,将$args数组修改为如下所示:

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

保持代码的其余部分不变,它应该可以工作。
为了将来的参考,请参阅这里的所有WP_Query参数:http://codex.wordpress.org/Function_Reference/WP_Query#Parameters