WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] nav_menu doesn't appear in homepage (20 posts)

  1. adahhane
    Member
    Posted 2 years ago #

    I have a small problem with nav_menu that is not appear in my homepage after I created a new custom post types.

    This is code that i put it in my functions.php

    add_action('init', 'create_companies_type');
        function create_companies_type(){
        register_post_type('information', array(
         'label' => __('Nos informations'),
         'singular_label' => __('information'),
         'public' => true,
         'show_ui' => true,
         'capability_type' => 'post',
         'hierarchical' => false,
         'supports' => array('title', 'author', 'thumbnail','editor','custom-fields'),
        ));
    }
    
    add_filter( 'pre_get_posts', 'my_get_posts' );
    
      function my_get_posts( $query ) {
      if ( is_home())
      $query->set( 'post_type', array( 'information','post') );
    
      return $query;
    }

    If you can help me I'll really grateful. Thanks

  2. adahhane
    Member
    Posted 2 years ago #

    any help please !!

  3. Digital Raindrops
    Member
    Posted 2 years ago #

    Hi,

    Do you mean that the posts cannot be added to a custom manu, if so just add a new argument:

    'show_in_nav_menus' => true,

    HTH

    David

  4. adahhane
    Member
    Posted 2 years ago #

    Thanks Mr for your quickly answer,
    it's not a solution of my problem
    I try it but it doesn't work

    Thanks

  5. Digital Raindrops
    Member
    Posted 2 years ago #

    Hi,
    You have to create a custom menu,
    Go to Appearance > Menu, and see if the custom posts types are there, if they are create a menu and add them, then assign the menu to the template location.

    If you are not familiar with Custom Menu's then have a look at the screenshot, create a new menu [ + ], add the menu items, assign it to a template location.

    Screenshot the custom posts are 'Information Posts' and been added to the custom menu!

  6. adahhane
    Member
    Posted 2 years ago #

    I'm sorry if my problem not very explicit so,
    I've do some screenshot to explain it better.

    In HomePage: The nav_menu dosen't appear !
    Here
    In Other Page: it's work perfectly
    Here

    Thanks man for your answers

  7. Harrison O.
    Member
    Posted 2 years ago #

    Look at the source code of the homepage (from the browser) and see if the nav menu code (all menus) are there. If it is there, then we know it is related with the css/html. Sometimes that happens when leaving a open div or the nav menu doesn't have enough space for an extra menu (perhaps your new custom post type).

  8. adahhane
    Member
    Posted 2 years ago #

    Hello thanks for you answer,
    So, I looked in my code source and I think that all are correctly done.

    for homepage :
    https://gist.github.com/2690465

    for headerPage :
    https://gist.github.com/2690470

    for functions.php :
    https://gist.github.com/2690475

  9. Harrison O.
    Member
    Posted 2 years ago #

    I was afraid you were going to look at the source code on your files so I updated my post but probably too late.

    While looking at the home page on your browser, mouse right click and click on "view page source". that is where you should look to see if all menus are there. You can gthub that source code for us to take a look too.

  10. adahhane
    Member
    Posted 2 years ago #

    Ok, Thanks.
    Menu isn't appear in the source code

    source code

  11. Digital Raindrops
    Member
    Posted 2 years ago #

    Looking at the header.php code you have a lot of unclosed tags, as Hfort has said it looks like bad HTML with unclosed tags!

    No closing tag </div>

    <div id="catmenucontainer" class="auto-style2">

    Images are not closed:

    <img alt="" class="" longdesc="" src="">

    Should be closed:

    <img alt="" class="" longdesc="" src="" />
    <img alt="" class="" longdesc="" src=""></img>

    Also do you have two header files one without the menu?

    HTH

    David

  12. adahhane
    Member
    Posted 2 years ago #

    Hello, david
    Thanks man for your response.
    but the closing tag for </div> to close catmenucontainer div exists.
    for <img> if you looked them carfully you can understand that juste a commentaire with <!-- -->.
    so, I don't think that problem is caused by the bad HTML.

    Thanks a lot for your answer.

  13. Harrison O.
    Member
    Posted 2 years ago #

    Okay, then your post type code is causing conflicts with the nav menu.
    Replace your custom post type with this:
    http://pastebin.com/QAYYJ3Nd and see if it works.

  14. Digital Raindrops
    Member
    Posted 2 years ago #

    I don't think that problem is caused by the bad HTML

    View Source Code, copy it and paste it into the validator and you get 74 errors, with unclosed tags!

    Is the home page calling the same header file, the nav menu call has no conditional code in header.php, which may mean a different header file for the homepage?

    Like:
    header-home.php

    get_header('home');

    HTH

    David

  15. Harrison O.
    Member
    Posted 2 years ago #

    Hi david, yes he will have to take care of those code errors too. However, I noticed that in the source code, the Nav menu is empty.

    <div id="catmenu" class="menu-menu-container"><ul id="menu-menu" class="menu">????</ul></div>

    That means, it hasn't be processed. My guess is that the filter "add_filter( 'pre_get_posts', 'my_get_posts' );" is conflicting with the nav menu. I don't see the need of that filter. Just register the custom post type as usual and query the post type directly into the home template.

  16. adahhane
    Member
    Posted 2 years ago #

    @Hfort : The same problem
    I think it comes from this function to dispaly the new post type:
    https://gist.github.com/2690664
    because once I disable it the menu appears in homePage

    @David : I'll do what are you suggesting to me thanks.

  17. adahhane
    Member
    Posted 2 years ago #

    @Hfort: You are right ! if I remove the filter it works but, I can't get the posts refer to new post type that I created.

  18. adahhane
    Member
    Posted 2 years ago #

    I solved the problem with your help
    Thanks a lot to you @David and @Hfort
    I'm really grateful.

  19. thermatk
    Member
    Posted 1 year ago #

    If you still have the problem I'll just leave it here. May be some one else will need same help. Should be

    function my_get_posts( $query ) {
      if ( $query->is_main_query() and is_home() ) {
        $query->set( 'post_type', array( 'post', 'someotherposttypes') );
      }
      return $query;
    }

    Instead of just

    function my_get_posts( $query ) {
     if ( is_home()){
     $query->set( 'post_type', array( 'information','post') );
     }
     return $query;
    }

    If you want some explanations - pre_get_posts is called not only when loading the main query but when loading any of them and when nav menu is constructed your modifications for this exact query don't have sense.

  20. cam5
    Member
    Posted 1 year ago #

    @thermatk - Thanks for the post & the explanation. You saved me!

    If I were to paraphrase, could someone confirm?

    The pre_get_posts hook is called before every query - even nav queries - and so, asking a nav menu to include custom post types is nonsensical, and results in an error.

Topic Closed

This topic has been closed to new replies.

About this Topic