• jarvo1980

    (@jarvo1980)


    Hi All,

    I hope someone can help.

    i’ve got my categories set up as follows:
    Section 1
    – sub section a
    – sub section b
    – sub section c
    Section 2
    – sub section a
    – sub section b
    – sub section c
    Section 3
    – sub section a
    – sub section b
    – sub section c

    If you’re in sub section x, how do you get the parent id, i.e. Section 1,2 or 3?

    Any help is much appreciated as I’m still learning!

    Thanks

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

    (@jarvo1980)

    Solved thanks to some altered code from a previous post:

    <?php $this_category = get_category($cat); ?>
    	<!-- If category is parent, list it -->
    	<?php if ($this_category->category_parent == 0) { ?>
    		<?php #echo get_category_link($cat); ?>
    		<?php #echo $this_category->cat_name; ?>
    		<?php #echo $this_category->cat_name; ?>
    		<?php $this_category->category_parent = $cat; ?>
    	<?php } else { ?>
    	<!-- If category is not parent, list parent category -->
    	<?php $parent_category = get_category($this_category->category_parent); ?>
    
    		<?php #echo get_category_link($parent_category->cat_ID); ?>
    		<?php #echo $parent_category->cat_name; ?>
    		<?php #echo $parent_category->cat_name; ?>
    		<?php $getParentCatId = $parent_category->cat_ID; ?>
    		<?php #echo $getParentCatId; ?>
    	<?php } ?>

    I then pass the id where I need it:

    if ( is_category() ) {
    
    		#pass the id from above to here to create the correct menu
    		$parent = $getParentCatId;
    
    		$categories = get_categories('include='.get_query_var('cat'));
    
    		if ( $categories[0]->category_parent == $parent ) {
    		#echo 'category ' . $categories[0]->name . ' is a child of category ' . $parent;
    
    			query_posts('cat='.$cat.'&showposts=9999');
    			?>
    
    			<ul>
    			<?php
    				while (have_posts()) : the_post();
    			?>
    				<li><a  href="<?php the_permalink() ?>"><?php the_title();  ?></a></li>
    			<?php
    				endwhile;
    			?>
    			</ul>
    			<?php
    
    		}
    	}

    hope it may help someone else…

Viewing 1 replies (of 1 total)
  • The topic ‘show/get parent category id’ is closed to new replies.