Title: Error message= &quot;Warning: in_array() expects parameter 2 to be array, null given&quot;
Last modified: August 20, 2016

---

# Error message= "Warning: in_array() expects parameter 2 to be array, null given"

 *  Resolved [andre77](https://wordpress.org/support/users/andre77/)
 * (@andre77)
 * [15 years ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/)
 * WordPress generates the following message: “Warning: in_array() expects parameter
   2 to be array, null given in C:\wamp\…\wp-content\themes\themename\sidebar.php
   on line 22” (line 22 being the first row in the code)
 * And here below you can see the code:
 * if (is_page(25) || in_array(“25”,$post->ancestors)){ ?>
    <?php dynamic_sidebar(‘
   sidebarmenu_welcome’ ); ?>
 *  <?php } else { ?>
    <?php dynamic_sidebar( ‘sidebarmenu_other’ ); ?> <?php } ?
   >
 * (this code checks if I am on a certain page, or if the current page is an ancestor
   of that specific page)
 * The webpage I am working on has both static pages, and a blog. The above code
   works fine on all static pages, but on the Blog page, this error message comes
   up. And then I have configured the: “Front Page” and “Blog Page” through the “
   reading settings” in the Admin Panel.
 * i.e. for some reason this code doesnt work when displaying “posts” instead of
   pages.
 * Can you help me correct this code? So it works when both pages and posts are 
   displayed.

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/page/2/?output_format=md)

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [15 years ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107585)
 * On that page `$post->ancestors` doesn’t exist and is therefore not an array. 
   You can make it an array by putting this in front of it:
 *     ```
       $post_ancestors = ($post->ancestors) ? $post->ancestors : array();
       if (is_page(25) || in_array("25", $post_ancestors)){
       ```
   
 * But then in_array will be checking an empty array on the front page.
 *  Thread Starter [andre77](https://wordpress.org/support/users/andre77/)
 * (@andre77)
 * [15 years ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107717)
 * Thanks a lot for your answer!
 * I am not getting your code to work, but I am still trying out different things,-)
 * But how can the code be written differently. I see no need for the child pages
   to be an array at all…The reason why I have the “in_array” in my code, is simply
   because I got this example from another source, and it seems to work at least
   when loading “pages”. But the main thing I am after is to get WordPress to load
   different sidebars depending on which parent page (and their subpages) I am on–
   AND I want to be able to control in the same matter which sidebar gets loaded
   when viewing posts/blogg.
 * What code would you use in order to accomplish this?
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [15 years ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107723)
 *     ```
       <?php
         if(is_home() || is_front_page()) {
         //do stuff for Posts on the front page
         } else {
           if(is_page()){
             // do stuff for Pages
             if (is_page(25) || in_array("25",$post->ancestors)){
             // do stuff for Page 25 and its ancestors
             }
           }
           // do default stuff
         }
       ?>
       ```
   
 *  Thread Starter [andre77](https://wordpress.org/support/users/andre77/)
 * (@andre77)
 * [15 years ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107742)
 * Hey again!
 * I guess I should have been more specific in my last reply, because the setup 
   is more complex, and therefore your code isn’t specific enough to work. The webpage
   is built up as an CMS-webpage, consisting of mainly static pages. The pages are
   categorized in about seven cagegories, which have lots of sub/ancestor pages 
   on their own (in several levels deep). I am calling a specific for each and every
   one of those categories, who contain the navigation for that category. Therefor
   the sidebar navigation, must also be present when an ancestor page of a parent
   is loaded.
 * The webpage will also contain a traditional “blog”, showing only posts. And this
   blog will probably also need to have its own specific sidebar as well.
 * The code you provided, does not make any difference between the main static page,
   and the “blog” page. And for some reason the line in the context you gave, for
   example:
 * if (is_page(25) || in_array(“25”,$post->ancestors)){
    dynamic_sidebar( ‘sidebarmenu_about_us’);
 * …does not work. When I am on page 25, then no sidebar gets loaded at all. But
   a sidebar is beeing loaded otherwise, as far as the first 6 lines of the code.
 * If you could give me an exampel that would work for the above scenario, I would
   be really greatful!
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [15 years ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107743)
 * > The pages are categorized in about seven cagegories
 * Pages don’t have categories or do you categorize them in a different way or are
   they Posts?
 * for more conditional tags look here: [http://codex.wordpress.org/Conditional_Tags](http://codex.wordpress.org/Conditional_Tags)
 *  Thread Starter [andre77](https://wordpress.org/support/users/andre77/)
 * (@andre77)
 * [15 years ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107747)
 * When I said categorized, I was not refering to how WordPress gategorizes posts.
   I was simply trying to make a point that the menu system of the webpage is complex,
   and that I have manually grouped the static pages into many categories, using
   parent, and subpage.
 * I have looked at the conditional tags page in the wordpress Codex, but I havent
   seen any examples of what I am trying to achieve 🙁
 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [15 years ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107752)
 * Try testing the $post->ancestors
 *     ```
       if ( $post->ancestors && ( is_page(25) || in_array("25",$post->ancestors) ) ){
       ```
   
 * HTH
 * David
 *  Thread Starter [andre77](https://wordpress.org/support/users/andre77/)
 * (@andre77)
 * [15 years ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107753)
 * I tried your code, but it didnt work. And to me it seems to be a bit unlogical.
 * The page 25, is a parent page for several levels of ancestor pages.
 * So if I understand the code correctly it means that: if the current page is an
   ancestor, and also has the ID 25 – OR if it is an array of page 25, then load
   optional code.
 * To me, this looks like the statement will never return true, as the parent page(
   25) is not an ancestor page to begin with.
 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [15 years ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107762)
 * Ok!
 * IF `has ancestors array` AND `25` is in the `ancestor array` OR `is page 25`
 *     ```
       if ( $post->ancestors && in_array("25",$post->ancestors) || is_page(25) ){
       ```
   
 * HTH
 * David
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107764)
 * Code translation:
    1. The current page has ancestors and is page id 25 2. The
   current page has ancestors and the page’s id is listed in the array called 25
 * If statement 1 or 2 is true, load the optional code.
 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [14 years, 12 months ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107785)
 * I read it as running the if statement when the page id is 25 or when page id 
   25 is an ancestor of the current page.
 * Code translation:
    1. Does the current page have ancestors and is one ancestor
   page id 25? `$post->ancestors && in_array("25",$post->ancestors)`
 * 2. Does the current page have the page id 25?
    `is_page(25)`
 * If statement 1 or 2 is true, load the optional code.
 * **Overview:**
    Just for anyone else that may find this topic, this code is just
   for pages and does not work for categories.
 * If the page has parent pages then WordPress will return an `array` of page numbers,
   if not then WordPress will return a boolean `false`.
 * This is why the `in_array($needle,$haystack)` errors if no array ($haystack) 
   is passed to search in.
 * `Warning: in_array() expects parameter 2 to be array, null given`
 * **Structure:**
    parent 7 page “Sport” -child page 11 “Word Soccer” –child page**
   25 FIFA** —child page 21 FA —child page 23 EUFA —child page 27 CAF -child page
   17 “World Tennis” –child page 24 ITF —child page 37 USTA —child page 39 LTA
 *     ```
       <!-- Start Headline -->
       <?php if( $post->ancestors && in_array("25",$post->ancestors) || is_page(25) ): ?>
       <h1>Headline: FIFA in crisis</h1>
       <?php endif; ?>
       <!-- End Headline -->
       ```
   
 * Page 7 ancestors = 0 Will now not error or show headline
    Page 27 ancestors =
   array(25,11,7) Show headline = 25 in array Page 25 ancestors = array(11,7) Show
   headline = page id 25 Page 37 ancestors = array(24,17,7) Do not show headline
   = page id 25 not in array
 * HTH
 * David 🙂
 *  Thread Starter [andre77](https://wordpress.org/support/users/andre77/)
 * (@andre77)
 * [14 years, 12 months ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107818)
 * Thank you people, for your good explanations! I can’t say I understand all the
   details yet, but I am working on it ,-)
 * I was real happy to see that your code has eliminated the array error message!
   I can now load every page (almost), and their subpages – and the corresponding
   sidebar gets loaded as well. But for some reason, when it comes to the “blog”-
   page, the corresponding sidebar for that page ID doesnt load. Instead wordpress
   just loads the “else”-statement for the blog page. See bottom of code, the blog
   page has the ID 159, and is configured in the reading settings as the “posts 
   page”. If I instead configure the reading settings so that I no longer have page
   159 as the “posts page”, the code works – and the correct sidebar gets loaded.
   So the problem has obviously something to do with what WordPress puts into the
   concept of the “posts page”.
 * Any idea what to do?
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome).
   Please use the [pastebin](http://wordpress.pastebin.com/)]_
 *  Thread Starter [andre77](https://wordpress.org/support/users/andre77/)
 * (@andre77)
 * [14 years, 12 months ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107829)
 * Missed out, that a code cant be pasted right on. So hopefully the code get put
   in this time…
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome).
   Please use the [pastebin](http://wordpress.pastebin.com/)]_
 *  Thread Starter [andre77](https://wordpress.org/support/users/andre77/)
 * (@andre77)
 * [14 years, 12 months ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107830)
 * David : I read through your post again and I realize that I am not using the 
   code at all as you described it with:
 * Page 7 ancestors = 0 Will now not error or show headline
    Page 27 ancestors =
   array(25,11,7) Show headline = 25 in array Page 25 ancestors = array(11,7) Show
   headline = page id 25 Page 37 ancestors = array(24,17,7) Do not show headline
   = page id 25 not in array
 * But the last code I provided, works for displaying specific sidebars, for parent
   pages, and their ancestors (dont ask me how ,-)
 * But your example, would basically mean that every ancestors page ID would have
   to be hardcoded in the sidebar.php, which is not a good idea for practical reasons.
   The future administrators of this webpage need to be able to create new subpages
   in the admin panel, without having to make any change in the php-code.
 * So the code function, as I see it would have to be described as:
 * “if on this specific page > or any of its ancestor pages > then show this sidebar”
 * In that case, only the top level, parent pages, would have to have its ID in 
   the sidebar – and then the administrators can create as many ancestor pages as
   they want because the code would be applied to them as well.
 *  [Digital Raindrops](https://wordpress.org/support/users/adeptris/)
 * (@adeptris)
 * [14 years, 12 months ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/#post-2107831)
 * Headline was just an example, the reason for the example was an overview of the
   logic.
 * If I was doing the code I would add post and page custom meta fields with a list
   of sidebars, using template parts to load the selected sidebars!
 * HTH
 * David

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/page/2/?output_format=md)

The topic ‘Error message= "Warning: in_array() expects parameter 2 to be array, 
null given"’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 16 replies
 * 4 participants
 * Last reply from: [andre77](https://wordpress.org/support/users/andre77/)
 * Last activity: [14 years, 11 months ago](https://wordpress.org/support/topic/error-message-warning-in_array-expects-parameter-2-to-be-array-null-given/page/2/#post-2107870)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
