WordPress.org

Ready to get started?Download WordPress

Forums

Warning message "Warning: in_array() expects parameter 2 to be array, null given (9 posts)

  1. AdreaUI
    Member
    Posted 1 year ago #

    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.

  2. s_ha_dum
    Member
    Posted 1 year ago #

    Check that $ancestors is an array before trying to use it:

    if ( is_array($ancestors) && in_array($post_id, $ancestors) ) {
  3. AdreaUI
    Member
    Posted 1 year ago #

    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.

  4. Andrew
    Forum Moderator
    Posted 1 year ago #

    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;
        }
    }
  5. s_ha_dum
    Member
    Posted 1 year ago #

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

  6. AdreaUI
    Member
    Posted 1 year ago #

    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.

  7. s_ha_dum
    Member
    Posted 1 year ago #

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

  8. AdreaUI
    Member
    Posted 1 year ago #

    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.

  9. s_ha_dum
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic