【问题标题】:How to create a sub menu with custom post types?如何使用自定义帖子类型创建子菜单?
【发布时间】:2013-02-28 07:27:50
【问题描述】:

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

我该如何解决这个问题?

【问题讨论】:

    标签: wordpress wordpress-theming submenu


    【解决方案1】:

    答案在WordPress Documentation

    Function Reference/wp list pages

    用法
    &lt;?php wp_list_pages( $args ); ?&gt;

    默认用法

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

    【讨论】:

      猜你喜欢
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 2020-01-19
      • 2015-06-26
      • 2016-05-13
      相关资源
      最近更新 更多