WordPress.org

Ready to get started?Download WordPress

Forums

Only Show SubCategory under parent SubCategory (2 posts)

  1. multari_r
    Member
    Posted 5 months ago #

    Hi,

    All of the subcategories show up under the "grandparent" category. I'm trying to only show the sub-subcategories under their "parent" subcategory.

    Any help would be greatly appreciated.

    Below is the code I'm using in loop-category.php.

    <?php
    global $query_string;
    query_posts( $query_string . '&orderby=date&order=DESC' );
    ?>
    <?php if (have_posts()){?>
    <p><span style="font-weight:bold">Sort by Date:</span> <a href="<?php echo str_replace(('?'.$_SERVER['QUERY_STRING']), '', $_SERVER['REQUEST_URI']).'/?sort=date'; ?>">Newest</a> &nbsp;|&nbsp; <a href="<?php echo str_replace(('?'.$_SERVER['QUERY_STRING']), '', $_SERVER['REQUEST_URI']).'/?sort=date2'; ?>">Oldest</a> &nbsp; &nbsp; &nbsp; <span style="font-weight:bold">Sort by Title:</span> <a href="<?php echo str_replace(('?'.$_SERVER['QUERY_STRING']), '', $_SERVER['REQUEST_URI']).'/?sort=title'; ?>">A to Z</a> &nbsp;|&nbsp; <a href="<?php echo str_replace(('?'.$_SERVER['QUERY_STRING']), '', $_SERVER['REQUEST_URI']).'/?sort=title2'; ?>">Z to A</a></p>
         <?php } ?>
         <?php if($_GET['sort'] == 'date') query_posts( $query_string . '&orderby=date&order=DESC' );  ?>
         <?php if($_GET['sort'] == 'title') query_posts( $query_string . '&orderby=title&order=ASC' );  ?>
         <?php if($_GET['sort'] == 'date2') query_posts( $query_string . '&orderby=date&order=ASC' );  ?>
         <?php if($_GET['sort'] == 'title2') query_posts( $query_string . '&orderby=title&order=DESC' );  ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();?>
    <div class="home_closed">
        <div class="home_closed_cap"></div>
        <a href="<?php the_permalink() ?>" class="home_closed_left_cap" rel="bookmark">
            <div class="even">
                <h3><?php the_title(); ?></h3>
                    <p class="home_closed_posted_date">Posted <?php the_time('F j, Y') ?></p>
                    <?php the_excerpt(); ?>
            </div>
        <div class="home_closed_cap"></div>
        </a>
    </div>
    <?php endwhile; endif; ?>

    Thank you in advance!

  2. multari_r
    Member
    Posted 5 months ago #

    Part of my problem is that I can get the subcategories to show in the hierarchy they are supposed to be in in but then the posts won’t show.

    I think the key is to combine these two pieces of code.

    <?php
    global $query_string;
    query_posts( $query_string . '&orderby=date&order=DESC' );
    ?>
    <?php if (have_posts()){?>
    <p><span style="font-weight:bold">Sort by Date:</span> <a href="<?php echo str_replace(('?'.$_SERVER['QUERY_STRING']), '', $_SERVER['REQUEST_URI']).'/?sort=date'; ?>">Newest</a> &nbsp;|&nbsp; <a href="<?php echo str_replace(('?'.$_SERVER['QUERY_STRING']), '', $_SERVER['REQUEST_URI']).'/?sort=date2'; ?>">Oldest</a> &nbsp; &nbsp; &nbsp; <span style="font-weight:bold">Sort by Title:</span> <a href="<?php echo str_replace(('?'.$_SERVER['QUERY_STRING']), '', $_SERVER['REQUEST_URI']).'/?sort=title'; ?>">A to Z</a> &nbsp;|&nbsp; <a href="<?php echo str_replace(('?'.$_SERVER['QUERY_STRING']), '', $_SERVER['REQUEST_URI']).'/?sort=title2'; ?>">Z to A</a></p>
    <?php } ?>
    <?php
        if($_GET['sort'] == 'date') query_posts( $query_string . '&orderby=date&order=DESC' );
        if($_GET['sort'] == 'title') query_posts( $query_string . '&orderby=title&order=ASC' );
        if($_GET['sort'] == 'date2') query_posts( $query_string . '&orderby=date&order=ASC' );
        if($_GET['sort'] == 'title2') query_posts( $query_string . '&orderby=title&order=DESC' );
        if (have_posts()) : while (have_posts()) : the_post();
    ?>
    <div class="home_closed">
        <div class="home_closed_cap"></div>
        <a href="<?php the_permalink() ?>" class="home_closed_left_cap" rel="bookmark">
            <div class="even">
                <h3><?php the_title(); ?></h3>
                <p class="home_closed_posted_date">Posted <?php the_time('F j, Y') ?></p>
                <?php the_excerpt(); ?>
            </div>
        <div class="home_closed_cap"></div>
        </a>
    </div>
    <?php endwhile; endif; ?>

    and

    <?php
    if(is_category()) {
    
    	$breakpoint = 0;
    	$thiscat = get_term( get_query_var('cat') , 'category' );
    	$subcategories = get_terms( 'category' , 'parent='.get_query_var('cat') );
    
    	if(empty($subcategories) && $thiscat->parent != 0) {
    		$subcategories = get_terms( 'category' , 'parent='.$thiscat->parent.'' );
    	}
    
    	$items='';
    	if(!empty($subcategories)) {
    		foreach($subcategories as $subcat) {
    			if($thiscat->term_id == $subcat->term_id) $current = ' current-cat'; else $current = '';
    			$items .= '
    <div class="home_closed">
        <div class="home_closed_cap"></div>
        <a href="'.get_category_link( $subcat->term_id ).'" title="'.$subcat->description.'" class="home_closed_left_cap" rel="bookmark">
            <div class="even">
                <h3>'.$subcat->name.' ('.$subcat->count.' posts)</h3>
                <p class="home_closed_posted_date">Posted '.get_the_time('F j, Y').'</p>
            </div>
        <div class="home_closed_cap"></div>
        </a>
    </div>
    			';
    		}
    		echo "$items";
    	}
    	unset($subcategories,$subcat,$thiscat,$items);
    }
    
    else {
    	// If no current cat query, just get the top level ones using wp_list_categories.
    	?>
    	<ul>
    		<?php wp_list_categories('title_li=&depth=1');?>
    	</ul>
    	<?php
    }
    ?>

    I’m wonder if some kind of an "if/then" statement might work but I’m more of a front end developer with a little backend experience. Usually I can fumble my way through but I can’t figure this one out for the life of me.

Reply

You must log in to post.

About this Topic