duanliang9288 2013-06-19 16:04 采纳率: 100%
浏览 13
已采纳

Wordpress中菜单项的分层列表

I'm trying to make a list of menuitems based on the page I'm viewing. For example if I'm on the page History, I have a menu with all the subcategories under history and the posts in these subcategories. Also the menuitems have to be clickable.

So basically I want this:

MAIN CAT = pagename

SUB CAT1
- post1
- post2

SUB CAT2
- post1
- post2

Here's what I've hacked together so far:

<div id="menu">
<ul>
    <?php
    $page_title = wp_title();
    preg_replace( "/[^a-z0-9 ]/i", "", $page_title);
    strtolower($page_title);

    $cat_id = get_cat_ID($page_title);
    //get terms (e.g. categories or post tags), then display all posts in each retrieved term
    $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'child_of' => '$cat_id'
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'showposts' => -1,
          'ignore_sticky_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {

          echo '<li><a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name. '</a> ';

          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <ul><li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li></ul>
           <?php
                 endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
</ul>

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • duanmei1850 2013-07-04 02:03
    关注

    I figured it out myself!

    <div id="menu">
    <ul>
    <?php
    $page_title = strtolower(wp_title( '', false, 'right' ));
    $clean = str_replace('&#8217;', '', $page_title);
    $clean = preg_replace('/[^A-Za-z0-9\-]/', '', $clean);
    $category = get_category_by_slug($clean);
    
    if ($category != null) {
    global $cat_id;
    $cat_id = $category->term_id;
    
    //get terms (e.g. categories or post tags), 
    //then display all posts in each retrieved term
    $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'child_of' => $cat_id
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'showposts' => -1,
          'ignore_sticky_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
    
          echo '<li>'. $term->name. '</li>';
    
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <ul><li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li></ul>
           <?php
                 endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
     }//endif
    ?>
    </ul>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂