WordPress.org

Ready to get started?Download WordPress

Forums

Only show sub-nav when in category page/post? (3 posts)

  1. calebo
    Member
    Posted 1 year ago #

    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>
  2. Andrew
    Forum Moderator
    Posted 1 year ago #

    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

  3. damlaetiket
    Member
    Posted 1 year ago #

    Thanks for reply.

Topic Closed

This topic has been closed to new replies.

About this Topic