• The site I am working on has many pages with many menus. I currently have the parent pages and their ancestors showing the pertinent menu according to code in the sidebar.php and the following function:

    // Check if page is an ancestor
    function is_ancestor($post_id) {
        global $wp_query;
        $ancestors = $wp_query->post->ancestors;
        if ( in_array($post_id, $ancestors) ) {
            return true;
        } else {
            return false;
        }
    }

    My news page (where posts are displayed) is a child of About. When I try to call the About Menu as per the rest of the site I get the following warning:

    Warning: in_array() expects parameter 2 to be array, null given in /Users/…./functions.php on line 138

    I am working locally so unfortunately I cannot provide a link to the site.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Check that $ancestors is an array before trying to use it:

    if ( is_array($ancestors) && in_array($post_id, $ancestors) ) {
    Thread Starter AdreaUI

    (@adreaui)

    Sorry for my ignorance. I am learning pHp as I go.

    Where should I put this statement? In functions.php or within the sidebar.php where I am trying to call the menu? How would I specifically check the news page? The above function works perfectly for the rest of the site. It is only the posts page (news) that I get the warning on.

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    You prepend your current if condition to consider checking for an array,
    E.g your code should be

    // Check if page is an ancestor
    function is_ancestor($post_id) {
        global $wp_query;
        $ancestors = $wp_query->post->ancestors;
        if ( is_array($ancestors) && in_array($post_id, $ancestors) ) {
            return true;
        } else {
            return false;
        }
    }

    Yes, what I gave you was a rewrite of one of the lines of code that you provided.

    Thread Starter AdreaUI

    (@adreaui)

    Okay gotcha. I amended the code and the warning disappeared. Great!

    Now knowing this what would be the recommended way to call the About Menu? This page is still, for all intents and purposes, a child page of About. I still need to call the About Menu but have to do it differently than the current mechanism used for all other pages.

    How did you make your blog index a ‘child’ of your about page?

    Thread Starter AdreaUI

    (@adreaui)

    I say it’s a child page because it is not a link on the main nav but rather you have to first go to About. Then there is a sub-nav with a News link.

    I have the respective menus showing on the rest of the site by using the following code:

    <?php
    if ( is_page( 'solutions' )) {
    	wp_nav_menu(array('menu'=>'solutions-menu' ));
    }
    elseif ( is_page( 'webcasts' ) || is_ancestor( '17' ) ) {
    	wp_nav_menu(array('menu'=>'webcasts-menu' ));
    }
     ?>

    This doesn’t seem to be working with the posts page. I also tried naming it directly e.g.

    <?php
    if ( is_page( 'solutions' )) {
    	wp_nav_menu(array('menu'=>'solutions-menu' ));
    }
    elseif ( is_page( 'news' )) {
    	wp_nav_menu(array('menu'=>'about-menu' ));
    }
     ?>

    This also doesn’t seem to work. I am getting a menu but not the correct one. Instead I am getting a menu consisting of News and Press Releases. Press Releases is the other post page I have on this site. I use categories to post each to it’s own page.

    Ok. You are talking about how you have your links setup, about how your front end user interface is constructed. I need to know how the pages and posts are setup in the backend. I need to know how it looks to the software and how it looks to the server, not how you lead users to it.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Warning message "Warning: in_array() expects parameter 2 to be array, null given’ is closed to new replies.