Become a MacRumors Supporter for $25/year with no ads, private forums, and more!
  • Did you order new AirTags? We've opened a dedicated AirTags forum.

definitive

macrumors 68000
Original poster
Aug 4, 2008
1,988
759
Not sure how to explain this, so bear with me... I have two different pages (each has its own category name 1) news 2) blog), and both of these pages have three sub-categories for types of posts. These sub-category names are shown on the left side of the page. When someone visits either of the two pages, they see all of the posts from that page's sub-category. If they click on one of the sub-category names on the left, then only that set of posts loads.

Posts are styled through their own php, and so are the pages. I also have index.php which contains the following:

Code:
	<?php
		//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',
		  [b]'child_of' => 123[/b]
		);
		$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,
		      'caller_get_posts'=> 1
		      );
		    $my_query = null;
		    $my_query = new WP_Query($args);
 		   if( $my_query->have_posts() ) {

  		    echo '<h1 style="text-decoration:none;padding-bottom:30px;font-weight:normal;"><a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name. '</a></h1> ';

  		    while ($my_query->have_posts()) : $my_query->the_post(); ?>
  		     <?php
             endwhile;
 		   }
		  }
		}
		wp_reset_query();
	?>

The index.php only lists the category for News (123), so if I visit the Blog (456) page, then it will still list News' sub-category links, so it wouldn't load the correct posts when I click on one sub-category. Is there some way to get this working? I have them set to 123 and 456 in their "template name: " and "template posts: " files, but index.php is giving me issues, since it's just one file.

Basically I'd like to display the sub-category names&links of the currently activate page on the left, and allow the links to take me to their appropriate sub-category posts.

Edit: I tried to replace 123 with $parent, but that shows all of the sub-categories, not just the three for the particular page.
 
Last edited:

960design

macrumors 68040
Apr 17, 2012
3,223
1,113
Destin, FL
I seriously tried to follow that, drank some port and tried again, little more port... still nothing. I'll keep drinking until I figure out what you are looking to do.

I may be totally off here, but could you just build a custom template page for each; throw in a couple of functions and be done with it?
 
Comment

definitive

macrumors 68000
Original poster
Aug 4, 2008
1,988
759
I seriously tried to follow that, drank some port and tried again, little more port... still nothing. I'll keep drinking until I figure out what you are looking to do.

I may be totally off here, but could you just build a custom template page for each; throw in a couple of functions and be done with it?

Yeah, I realize that it was a bit difficult for me to describe what I was trying to do. I already had a separate template for each page, but needed to somehow unify them through index.php. Ended up finding a solution after some searching and trial & error:

Code:
<?php
    echo '<ul>';
    $post_child_cat = array();
    foreach((get_the_category()) as $cat) {
        $args = array( 'child_of' => $cat->cat_ID );
        $categories = get_categories( $args );
        if( $categories ) foreach( $categories as $category ) {
            echo '<li class="cat-item cat-item-'.$category->term_id.'">'.
            '<a title="'.$category->description.'" href="';
            echo bloginfo('url');
            echo '/category/'.$cat->slug.'/'.$category->slug.'">'.
            $category->name.'</a></li>'; 
        }
    }
   echo '</ul>';
?>

This loop now allows me to display a list of sub-categories/children based on the page/parent-category that a user is currently viewing. So for example if they're on page called Page 1, it will display the sub-categories associated with that page, and same will happen on Page 2. Both will be controlled from index.php file.
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.