• davehouse

    (@davehouse)


    Hi all,

    A clients magazine-style site uses WordPress as a CMS. I have a page that displays links to the articles for each back issue in a dropdown list in a modified sidebar. Each back issue has its own category (issue1, issue2, etc) which article posts are assigned to. This is all working fine.

    I now need to add subcategories to some back issues and include these hierarchically in the dropdown. The resulting system should work like this:

    • Issue 1 (parent category name)
    • Standard articles (child category name)
    • Article 1
    • Article 2
    • Special articles (child category name)
    • Article 1
    • Article 2
    • Issue 2 (parent category name)
    • Standard articles (child category name)
    • Article 1
    • Article 2
    • Special articles (child category name)
    • Article 1
    • Article 2

    If there are no Special Articles present for an issue then that category shouldn’t show up in the drop down. Standard articles will always be present.

    The code I have working at present for the dropdowns without subcategories is as follows. I’ve found code that comes close to doing what I want, but my PHP isn’t good enough to fully integrate it.

    <ul id="dropdown">
    <?php
    //get all categories then display all posts in each term
    $taxonomy = 'category';
    $param_type = 'category__in';
    $term_args=array(
      'orderby' => 'name',
      'include' => '33,34,35,36,37,38,39,40,41,42,43,44', // these are the back issue parent category numbers
      'order' => 'ASC'
    );
    $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',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $categories =  get_categories('child_of='.$term_id);
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {  ?>
    
    	<li><a href="#"><strong><?php echo $term->name;?></strong><img src="images/down_arrow.gif" width="8" height="7" alt="arrow"></a>
    		<ul id="sub">
                    <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
    		      	<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?><?php if ( get_post_meta($post->ID, 'By', true) ) { ?>, by <?php echo get_post_meta($post->ID, 'By', true) ?><? } ?></a></li>
    
    		<?php endwhile; ?>
    		</ul>
    	</li>
     <?php
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post(). ?>
    </ul>

    The website is http://www.shoutaboutmagazine.com, but I’ve got maintenance mode engaged so you can’t see it…! If you want to take a look I can disengage it.

The topic ‘Adding child categories to drop down posts list’ is closed to new replies.