Andrea Visibelli
Andrea Visibelli

Reputation: 61

How to create a sub menu with custom post types?

I built a website with WordPress and I have custom posts types. In some pages I have the subnavigation and I want to show it in the page like in the follow image:

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

it works fine, isn't it?

Yes but if you try to see "Vini" page as in the follow image you can't see the sub-menu:

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

The difference between the two submenus is that the first is filled of base pages of WordPress while the second is filled of custom post pages!

MY CODE IS:

  <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>

How can I solve this problem?

Upvotes: 2

Views: 1191

Answers (1)

brasofilo
brasofilo

Reputation: 26065

The answer is in WordPress Documentation:

Function Reference/wp list pages

Usage:
<?php wp_list_pages( $args ); ?>

Default usage:

<?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' 
); ?>

As you can see, the parameter post_type is the one you need to achieve a Custom Post Type listing. Simply use your CPT slug.

Upvotes: 1

Related Questions