WordPress.org

Ready to get started?Download WordPress

Forums

loop causes page menu to not be active (5 posts)

  1. ottomek
    Member
    Posted 1 year ago #

    Here is the default page loop:

    <?php
    				if(have_posts()) {
    					/* Start the Loop */
    					while (have_posts()) {
    						the_post();
    						get_template_part('content', 'page');
    						/* Display comments */
    						if ( theme_get_option('theme_allow_comments')) {
    							comments_template();
    						}
    					}
    
    				} else {
    					 theme_404_content();
    
    				}
    		    ?>

    When I modify it to this code below in a custom page template(to show a category of posts on a page), the menu item is not active anymore.

    <?php
    				if(have_posts()) {
    					/* Start the Loop */
                        query_posts('cat=51');
    					while (have_posts()) {
    						the_post();
    						get_template_part('content', 'post');
    						/* Display comments */
    						if ( theme_get_option('theme_allow_comments')) {
    							comments_template();
    						}
    					}
    				}
    		    ?>

    Is there something I can add to the custom page template or to the loop above to get the page menu item to remain active?

    thanks,
    John

  2. ottomek
    Member
    Posted 1 year ago #

    I forgot to mention that using the edited loop removes the class="active" from the Menu ul & li, see image.

    code image

    thanks,

  3. thejaeck
    Member
    Posted 1 year ago #

    Hey man i had the same problem a couple of minutes ago.

    It should work as follows:

    <?php
    				if(have_posts()) {
    					/* Start the Loop */
                        query_posts('cat=51');
    					while (have_posts()) {
    						the_post();
    						get_template_part('content', 'post');
    						/* Display comments */
    						if ( theme_get_option('theme_allow_comments')) {
    							comments_template();
    						}
    					}
    				}
    
    				// go backwards to the original post for the custom menu
    				wp_reset_query();
    				if(have_posts()) : the_post(); endif;
    
    ?>
  4. ottomek
    Member
    Posted 1 year ago #

    thejaeck,
    Thank you! That works perfectly, I appreciate you posting this.

    John

  5. ottomek
    Member
    Posted 1 year ago #

    Maybe this question should be in another post. I'm using the code above which pulls the Page content and list of posts and want to have the nice pagination from pageNavi plugin at the bottom for the posts.

    http://wordpress.org/extend/plugins/wp-pagenavi/

    How could I add that so it shows just archive/older posts for category id=51?

    Thoughts? thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic