0

我正在尝试根据我正在查看的页面制作菜单项列表。例如,如果我在历史页面上,我有一个菜单,其中包含历史下的所有子类别以及这些子类别中的帖子。菜单项也必须是可点击的。

所以基本上我想要这个:

MAIN CAT = pagename

SUB CAT1
- post1
- post2

SUB CAT2
- post1
- post2

到目前为止,这是我一起破解的内容:

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

提前致谢!

4

2 回答 2

1

我之前用过这个,希望对你有帮助。

<?php
    $current_id = $post->ID;
    $args = array(
        'orderby' => 'name',
        'order' => 'ASC',
    );

    $nav_query = new WP_Query( $args );

?>

<ul>

<?php if ( $nav_query -> have_posts() ) : while ( $nav_query->have_posts() ) : $nav_query -> the_post(); ?>

    <li class="clearfix">

        <?php $current_class = ( $current_id == $post->ID ) ? 'class="current"' : ''; ?>
        <a <?php if ( $current_class ) echo $current_class; ?> href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

    </li>

<?php endwhile; ?>

</ul>

<?php endif; wp_reset_query(); ?>
于 2013-06-21T13:14:38.217 回答
0

我自己想通了!

<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>
于 2013-07-04T02:03:51.193 回答