WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Error message= "Warning: in_array() expects parameter 2 to be array, null given" (17 posts)

  1. andre77
    Member
    Posted 2 years ago #

    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.

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    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.

  3. andre77
    Member
    Posted 2 years ago #

    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?

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    <?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
      }
    ?>
  5. andre77
    Member
    Posted 2 years ago #

    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!

  6. keesiemeijer
    moderator
    Posted 2 years ago #

    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

  7. andre77
    Member
    Posted 2 years ago #

    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 :-(

  8. Digital Raindrops
    Member
    Posted 2 years ago #

    Try testing the $post->ancestors

    if ( $post->ancestors && ( is_page(25) || in_array("25",$post->ancestors) ) ){

    HTH

    David

  9. andre77
    Member
    Posted 2 years ago #

    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.

  10. Digital Raindrops
    Member
    Posted 2 years ago #

    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

  11. esmi
    Theme Diva & Forum Moderator
    Posted 2 years ago #

    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.

  12. Digital Raindrops
    Member
    Posted 2 years ago #

    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 :)

  13. andre77
    Member
    Posted 2 years ago #

    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. Please use the pastebin]

  14. andre77
    Member
    Posted 2 years ago #

    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. Please use the pastebin]

  15. andre77
    Member
    Posted 2 years ago #

    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.

  16. Digital Raindrops
    Member
    Posted 2 years ago #

    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

  17. andre77
    Member
    Posted 2 years ago #

    Hey David!

    Sorry for the late reply, been away for some days. Using custom meta fields is an idea that I didnt think of. That could give the administrative users more flexibility, but if such a high flexibility isn't needed, then I guess it would also result in a slightly more administrative work - as every new sub page would need have to have a custom field assigned to it to display the appropriate sidebar.

    But I guess the possibilities are quite many if you know all the ropes of WordPress / PHP.

    But the initial question/problem has been solved - thanks to your good effort :-)

    Kind regards,

    André

Topic Closed

This topic has been closed to new replies.

About this Topic