• Here is my current code, it list all the category as page navigation and then post under each category as sub nav items.

    But how can I make it only show sub nav when i’m viewing that category page/post?

    <ul id="menu-navigation" class="menu">
    			<?php // get all the categories from the database
    			$cats = get_categories();
    				// loop through the categries
    				foreach ($cats as $cat) {
    					// setup the category ID
    					$cat_id = $cat->term_id;
    					// Make a header for the cateogry
    					echo "<li><a href=\".$get_site_url./".strtolower($cat->name)."\">".$cat->name."</a>";
    					// create a custom wordpress query
    					$catposts = new WP_Query( array( 'category__in' => array($cat_id), 'posts_per_page' => 10) );
    					// start the wordpress loop!
    					if ( $catposts->have_posts() ) : ?>
    						<ul>
    						<?php while( $catposts->have_posts() ) : $catposts->the_post(); ?>
    						<?php // create our link now that the post is setup ?>
    						<li><a href="<?php the_permalink();?>"><?php echo $catposts->current_post+1; ?></a></li>
    						<?php endwhile; ?>
    						</ul></li>
    					<?php endif; wp_reset_postdata(); // done our wordpress loop. Will start again for each category ?>
    				<?php } // done the foreach statement ?>
    				<li><a href="<?php bloginfo('url'); ?>/contact">Contact</a></li>
    			</ul>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    You could try wrapping your sub menu navigation in a conditional statement, such as is_category().
    E.g

    <?php if ( is_category() ) { ?>
    <ul id="menu-navigation" class="menu">
    			<?php // get all the categories from the database
    			$cats = get_categories();
    				// loop through the categries
    				foreach ($cats as $cat) {
    					// setup the category ID
    					$cat_id = $cat->term_id;
    					// Make a header for the cateogry
    					echo "
    <li><a>name)."\">".$cat->name."</a>";
    					// create a custom wordpress query
    					$catposts = new WP_Query( array( 'category__in' => array($cat_id), 'posts_per_page' => 10) );
    					// start the wordpress loop!
    					if ( $catposts->have_posts() ) : ?>
    
    <ul>
    						<?php while( $catposts->have_posts() ) : $catposts->the_post(); ?>
    						<?php // create our link now that the post is setup ?>
    
    <li><a>"><?php echo $catposts->current_post+1; ?></a></li>
    						<?php endwhile; ?>
    						</ul>
    </li>
    					<?php endif; wp_reset_postdata(); // done our wordpress loop. Will start again for each category ?>
    				<?php } // done the foreach statement ?>
    
    <li><a>/contact">Contact</a></li>
    <?php } ?>

    Check out Codex’s category function list
    http://codex.wordpress.org/Function_Reference/#Category.2C_Tag_and_Taxonomy_Functions

    Thanks for reply.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Only show sub-nav when in category page/post?’ is closed to new replies.