2011-04-20 172 views
0

我使用以下代码在父页面上显示子页面的列表以及侧边栏中的子页面的精选后期图像。WordPress - 显示子页面(标题和缩略图)ON子页面

<?php $args = array(
     'orderby' => 'menu_order', 
     'order' => 'ASC', 
     'post_parent' => $post->ID, 
     'post_type' => 'page', 
     'post_status' => 'publish' 
     ); 
     $postslist = get_posts($args); 
     foreach ($postslist as $post) : setup_postdata($post); 
    ?> 
    <div class="top10"> 
    <a href="<?php the_permalink();?>"> 
    <?php the_post_thumbnail('large'); ?> 
    </a> 
    </div> 
    <?php endforeach; ?> 
    <?php wp_reset_query(); ?> 

但是,我也需要在边栏中显示相同的列表,当打开一个子页面时。目前,在没有调整的情况下使用相同的代码时,在子页面上不显示任何内容。

我尝试改变行''post_parent'=> $ post-> ID,“to”'post_parent'=> $ post-> ID。“echo = 0”,“其中显示了一些子页面,但不是全部,所以我显然搞砸了。

有人可以帮助我修改代码来处理父级子页面以及父级子页面吗?

感谢 扎克

+0

你能不能一个使用WordPress的菜单吗? – 2011-04-20 10:15:23

+0

一个WordPress菜单通常只显示页面标题,而方法演示允许我拉动缩略图,标题,即使是我想要的摘录! – 2011-04-20 10:19:03

+0

所以你遇到的问题是自动获取父页面ID?我对吗? – 2011-04-20 10:22:13

回答

1

使用此功能可为您的菜单中的ID。它将确定页面是否具有父级,并使用该ID,否则返回当前页面ID。

function get_menu_id(){ 
    if ($post->post_parent) { 
     $parent = get_post_ancestors($post->ID); 
     return $parent[0]; 
    } else { 
     return $post->ID; 
    } 
} 

全码

<?php 


function get_menu_id(){ //this function would be better off in your functions.php file 
     if ($post->post_parent) { 
      $parent = get_post_ancestors($post->ID); 
      return $parent[0]; 
     } else { 
      return $post->ID; 
     } 
    } 
$args = array(
     'orderby' => 'menu_order', 
     'order' => 'ASC', 
     'post_parent' => get_menu_id(), 
     'post_type' => 'page', 
     'post_status' => 'publish' 
     ); 
     $postslist = get_posts($args); 
     foreach ($postslist as $post) : setup_postdata($post); 
    ?> 
    <div class="top10"> 
    <a href="<?php the_permalink();?>"> 
    <?php the_post_thumbnail('large'); ?> 
    </a> 
    </div> 
    <?php endforeach; ?> 
    <?php wp_reset_query(); ?> 
相关问题