如何使用自定义帖子类型创建子菜单?

时间:2013-03-12 16:40:57

标签: wordpress wordpress-theming submenu

我用WordPress建立了一个网站,我有自定义帖子类型。 在某些页面中,我有子导航,我想在页面中显示它,如下图所示:

http://www.andreavisibelli.com/screen-1.jpg

它运作正常,不是吗?

是,但如果您尝试查看“Vini”页面,如下图所示,则无法看到子菜单:

http://www.andreavisibelli.com/screen-2.jpg

两个子菜单之间的区别在于,第一个填充了WordPress的基页,而第二个填充了自定义帖子页面!

我的代码是:

  <ul id="subnavigation">
<?php
  if($post->post_parent)
  $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
  else
  $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
  if ($children) { ?>
  <ul>
  <?php echo $children; ?>
  </ul>
  <?php } ?>
  </ul>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

答案在WordPress Documentation

  

Function Reference/wp list pages

     

<强>用法
  <?php wp_list_pages( $args ); ?>

     

默认使用

<?php $args = array(
  'depth'        => 0,
  'show_date'    => '',
  'date_format'  => get_option('date_format'),
  'child_of'     => 0,
  'exclude'      => '',
  'include'      => '',
  'title_li'     => __('Pages'),
  'echo'         => 1,
  'authors'      => '',
  'sort_column'  => 'menu_order, post_title',
  'link_before'  => '',
  'link_after'   => '',
  'walker'       => '',
  'post_type'    => 'page',
  'post_status'  => 'publish' 
); ?>

如您所见,参数 post_type 是您获得自定义帖子类型列表所需的参数。只需使用您的CPT slug。