WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_nav_menu not working on homepage (5 posts)

  1. feintnate
    Member
    Posted 4 years ago #

    wp_nav_menu works on every other page of the website but not on the homepage.

    I have the site homepage set to be different from the directory I installed WordPress.

    Site is: http://www.natesiggard.com

    I think it is related to having the blog files in one folder and having it load in the parent folder (different blog and website addresses).

    Any advice to get this working would be great.

  2. feintnate
    Member
    Posted 4 years ago #

    Just put the index.php and .htaccess back into the blog folder, and had the same issue. Not related to the having the homepage outside the wordpress folder.

  3. garza
    Member
    Posted 4 years ago #

    feintnate, did you ever find the solution to this?

  4. MathSmath
    Member
    Posted 3 years ago #

    I just spent hours on the same symptom.

    Try disabling all plugins, and look through your functions.php file to see if you have some code that modifies queries.

    Don't know if your underlying issue will be the same, but here's what the problem was for me:

    I had code in my functions.php file that was meant to include custom content types in search results, feeds, etc. (this kind of code could also exist in a plugin you're using, which is why I suggested turning them off). My code looked like this:

    function include_post_types($query) {
    
    	global $wp_query;
    
    	if ( !is_preview() && !is_singular() && !is_admin() ) {
    
        	$post_types = array('post', 'press-releases');
    
    		$existing_post_type = get_query_var( 'post_type' ); // Only do this if the post type is not already set
    		if ( empty( $existing_post_type ) ) {
    
    			$query->set( 'post_type' , $post_types );
    
    		}
    
    	}
    
    	return $query;
    
    }
    add_filter('pre_get_posts' , 'include_post_types');

    Pretty sure I found this code (or something similar) on the forum somewhere, so it's possible other people are using it. The problem is, it assumes the global wp_query and the query being parsed by pre_get_posts are the same. But in custom queries using the WP_Query object (which the navigation widget and template tag uses), they're not necessarily the same, and you might accidentally overwrite valid query vars.

    I fixed it by doing something I should have done in the first place--removing the reference to the global $wp_query.

    function include_post_types( $query ) {
    
    	if ( !is_preview() && !is_singular() && !is_admin() ) {
    
        	$post_types = array('post', 'press-releases');
    
    		$existing_post_type = $query->query_vars['post_type']; // Only do this if the post type is not already set
    		if ( empty( $existing_post_type ) ) {
    
    			$query->set( 'post_type' , $post_types );
    
    		}
    
    	}
    
    	return $query;
    
    }
    add_filter('pre_get_posts' , 'include_post_types');

    Hope this helps!

  5. Stefan
    Member
    Posted 3 years ago #

    Hi MathSmath,

    you saved my day. I had exactly the same problem and found an old pre_get_posts-filter that added a new post_type that doesn't exist.

    I would never have guessed that this could have such an effect.
    Thanks for your hint.

Topic Closed

This topic has been closed to new replies.

About this Topic