Wordpress and categories (simple question?)

Discussion in 'Web Design and Development' started by definitive, Nov 19, 2013.

  1. definitive, Nov 19, 2013
    Last edited: Nov 19, 2013

    definitive macrumors 68000

    definitive

    Joined:
    Aug 4, 2008
    #1
    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.
     
  2. 960design macrumors 68020

    Joined:
    Apr 17, 2012
    Location:
    Destin, FL
    #2
    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?
     
  3. definitive thread starter macrumors 68000

    definitive

    Joined:
    Aug 4, 2008
    #3
    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.
     

Share This Page