• Resolved sephers2

    (@sephers2)


    Hi All,
    I’m current building a template for a site that’ll be displaying products. The idea being each post will be a product and it’ll filed under certain categories. So at the minute I have a ‘Display’ category and inside that are ‘LCD’,’LED’,’TFT’ subcategories. Then within ‘LCD’ I have the subcategories ‘Alphanumeric’ and ‘Graphic’.

    When a user clicks on the Products button I take them to the category page where the entire tree is show, ie all the top level categories along with the subcategories. The permalink for this is ‘/index.php/category/products/’. That works fine, however when I click display it won’t display the subcategories, when clicking displays it takes the user to ‘/index.php/category/products/displays/’.

    below is the coding for category.php for my template:

    <?php
    get_header();
    get_sidebar();
    ?>
    		<div id="content">
    			<?php
    			$catinfo = get_the_category();
    			$catslug = $catinfo[0]->slug;
    			$catid = $catinfo[0]->term_id;
    			$catname = $catinfo[0]->name;
    			?>
    				<div id="contentheader"><?php echo $catname ?></div><!--END #contentheader-->
    				<p>
    					<?php
    					if($catslug == 'news'){
        					$myposts = get_posts('numberposts=3&offset=0&'.$catshow);
    						foreach($myposts as $post): ?>
    							<li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li>
    						<?php endforeach; ?>
    					<?php }else{
    					wp_list_categories('title_li=&hide_empty=0&child_of='.$catid);
    					?>
    					<?php } ?>
    
    				</p>
    <?php get_footer(); ?>

    Just to say that if($catslug == 'news'){ is used to see if we are looking at the news category and if so just display the posts not the subcategories.
    My problem lies with $catinfo = get_the_category();

    This exists in ‘/index.php/products/’ but in ‘/index.php/products/display/’ it doesn’t exist meaning there is no child_of defined so all the categories are shown.

    Thanks for any help.
    Sephers.

Viewing 1 replies (of 1 total)
  • Thread Starter sephers2

    (@sephers2)

    No worries I found the answer, instead of using get_the_category, I used php functions to split the url up to see the end bit (the slug of the category) and then get the stuff based on that, below is my new code:

    <?php
    get_header();
    get_sidebar();
    ?>
    		<div id="content">
    			<?php
    			$self = $_SERVER['PHP_SELF'];
    			$self = basename($self);
    			$catinfo = get_term_by('slug', $self, 'category');
    			$catslug = $catinfo->slug;
    			$catid = $catinfo->term_id;
    			$catname = $catinfo->name;
    			?>
    				<div id="contentheader"><?php echo $catname ?></div><!--END #contentheader-->
    				<p>
    					<?php
    					if($catslug == 'news'){
        					$myposts = get_posts('numberposts=3&offset=0&'.$catshow);
    						foreach($myposts as $post): ?>
    							<li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li>
    						<?php endforeach; ?>
    					<?php }else{
    					wp_list_categories('title_li=&hide_empty=0&child_of='.$catid);
    					?>
    					<?php } ?>
    
    				</p>
    <?php get_footer(); ?>

Viewing 1 replies (of 1 total)

The topic ‘Displaying subcategories on category.php’ is closed to new replies.