WordPress.org

Ready to get started?Download WordPress

Forums

category.php for each sub-cat? (1 post)

  1. lorensson
    Member
    Posted 1 year ago #

    Hi guys and gals, I'm having difficulty figuring this out.

    I have a website whose theme is setup to display posts only when on a category, and I need to make this work automatically for each new 'city' category. My category structure currently looks like this, for example:

    - UK
    - - Bristol
    - - - bananas
    - - - apples
    - - London
    - - - bananas
    - - - apples
    - US
    - - Los Angeles
    - - - bananas
    - - - apples
    - - Portland
    - - - bananas
    - - - apples
    - BLOG
    - - blog cat 1
    - - bog cat 2

    I have setup the site so you can only access a category listing at the level of an individual city, meaning it appears I have a 'page' for each city, but it's technically just a category listing. I want my category template to be applied when viewing any city category.

    In my code sample below (category.php) you can see my problem seems to be on line 7, $cat_id = get_cat_ID('Bristol'); — I need this to basically grab each immediate child category under the UK and US categories instead of having to declare each city independently.

    Here is what I'm using now (a bunch of custom stuff, but applies to each city's sub-categories):

    <?php get_header(); ?>
    <?php if (cat_is_ancestor_of(33, $cat) or cat_is_ancestor_of(56, $cat)) { ?>
    <?php
    //get all children of category "Bristol", then display posts in each cat
    $taxonomy = 'category';
    $param_type = 'category__in';
    $cat_id = get_cat_ID('Bristol');
    $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',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo '<ul>
    		<li class="listheader"><h2>'.$term->name.'</h2></li>';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    		<li class="vcard">
    			<a class="url fn org" href="<?php echo get_post_meta($post->ID, 'url', true) ?>"><?php the_title(); ?></a>
    			<p class="note"><?php the_content_rss(); ?></p>
    
    			<?php if (get_post_meta($post->ID, 'facebook', true)) { ?>
    				<p><a class="facebook" href="<?php echo get_post_meta($post->ID, 'facebook', true) ?>">on Facebook</a></p>
    			<?php } ?>
    
    			<?php if (get_post_meta($post->ID, 'twitter', true)) { ?>
    				<p><a class="twitter" href="http://twitter.com/#!/<?php echo get_post_meta($post->ID, 'twitter', true) ?>">@<?php echo get_post_meta($post->ID, 'twitter', true) ?></a></p>
    			<?php } ?>
    
    			<?php if (get_post_meta($post->ID, 'flickr', true)) { ?>
    				<p><a class="flickr" href="<?php echo get_post_meta($post->ID, 'flickr', true) ?>">on Flickr</a></p>
    			<?php } ?>
    
    			<?php if (get_post_meta($post->ID, 'vimeo', true)) { ?>
    				<p><a class="vimeo" href="<?php echo get_post_meta($post->ID, 'vimeo', true) ?>">on Vimeo</a></p>
    			<?php } ?>
    
    			<?php if (get_post_meta($post->ID, 'rss', true)) { ?>
    				<p><a class="rss" href="<?php echo get_post_meta($post->ID, 'rss', true) ?>">RSS Feed</a></p>
    			<?php } ?>
    
    			<?php if (get_post_meta($post->ID, 'map', true)) { ?>
    				<p><a class="map" href="<?php echo get_post_meta($post->ID, 'map', true) ?>">on Google Maps</a></p>
    			<?php } ?>
    
    			<?php if (get_post_meta($post->ID, 'podcast', true)) { ?>
    				<p><a class="podcast" href="<?php echo get_post_meta($post->ID, 'podcast', true) ?>">Podcast</a></p>
    			<?php } ?>
    
    			<?php if (get_post_meta($post->ID, 'email', true) || get_post_meta($post->ID, 'phone-1', true) || get_post_meta($post->ID, 'phone-2', true)) { ?><p><?php } ?>
    				<?php if (get_post_meta($post->ID, 'email', true)) { ?>
    					<a class="email" href="mailto:<?php echo get_post_meta($post->ID, 'email', true) ?>"><?php echo get_post_meta($post->ID, 'email', true) ?></a>
    				<?php } ?>
    
    				<?php if (get_post_meta($post->ID, 'phone-1', true)) { ?>
    					<a class="tel" href="tel:<?php echo get_post_meta($post->ID, 'phone-1', true) ?>"><span class="type">Work</span><?php echo get_post_meta($post->ID, 'phone-1', true) ?></a>
    				<?php } ?>
    				<?php if (get_post_meta($post->ID, 'phone-2', true)) { ?>
    					<a class="tel" href="tel:<?php echo get_post_meta($post->ID, 'phone-2', true) ?>"><span class="type">Work</span><?php echo get_post_meta($post->ID, 'phone-2', true) ?></a>
    				<?php } ?>
    			<?php if (get_post_meta($post->ID, 'email', true) || get_post_meta($post->ID, 'phone-1', true) || get_post_meta($post->ID, 'phone-2', true)) { ?></p><?php } ?>
    
    			<?php if (get_post_meta($post->ID, 'address-1', true)) { ?>
    				<p class="adr">
    					<span class="street-address"><?php echo get_post_meta($post->ID, 'address-1', true) ?></span>
    			<?php } ?>
    			<?php if (get_post_meta($post->ID, 'address-2', true)) { ?>
    					<span class="extended-address"><?php echo get_post_meta($post->ID, 'address-2', true) ?></span>
    			<?php } ?>
    			<?php if (get_post_meta($post->ID, 'address-3', true)) { ?>
    					<span class="locality"><?php echo get_post_meta($post->ID, 'address-3', true) ?></span>,
    			<?php } ?>
    			<?php if (get_post_meta($post->ID, 'address-4', true)) { ?>
    					<span class="region"><?php echo get_post_meta($post->ID, 'address-4', true) ?></span>,
    			<?php } ?>
    			<?php if (get_post_meta($post->ID, 'address-5', true)) { ?>
    					<span class="postal-code"><?php echo get_post_meta($post->ID, 'address-5', true) ?></span>
    			<?php } ?>
    			<?php if (get_post_meta($post->ID, 'address-6', true)) { ?>
    					<span class="country-name"><?php echo get_post_meta($post->ID, 'address-6', true) ?></span>
    			<?php } ?>
    			<?php if (get_post_meta($post->ID, 'address-1', true)) { ?>
    				</p>
    			<?php } ?>
    		<?php echo '</li>'; ?>
           <?php
          endwhile;
        } echo '</ul>';
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    <?php } ?>
    <?php get_footer(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic