WordPress.org

Forums

[resolved] Using arrays with is_page() doesn't seem to work...help pls? (4 posts)

  1. f00bar
    Member
    Posted 4 years ago #

    Hi everyone,

    I'm working on a site that has child pages, and I'm needing to use them as links on a menu. I'm using wp_list_pages() for that.

    However, when I click a child page link, it will display the top level menu instead. So, having looked through the Function Reference I found is_page() and used the following code to get the correct page IDs:

    $aboutchildren=get_pages("child_of=53");
    $aboutlinks.=53;
    
    foreach($aboutchildren as $child) :
    	$aboutlinks.=",".(integer) $child->ID;
    endforeach;
    
    if(is_page(array($aboutlinks))) :
       //put the links for the about page here...
    else :
       //put main links.
    endif;

    Just to explain, 53 is the ID of the top level About page. However, event though the correct IDs are being passed in the $aboutlinks array, the code doesn't pick it up - it ONLY works if I literally write the array by hand (i.e. is_page(array(1,2,3,4,5)) etc.). Is there a reason why this won't work? I really don't want to have to hard code the IDs, in case they change or more pages are added.

    Any help on this would be greatly appreciated.

    Thank you
    Michael

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    That's because you only have one item in the array..

    Basically what you have above creates a string of numbers, ie '1,2,3', and then wraps them in an array, ie. array('1,2,3'), leaving your array with one item..

    Creating an array.

    $my_array = array();

    Adding to the array.

    $my_array[] = 33;
    
    $my_array[] = 55;
    
    ..and so on..

    Have made the same mistake myself in the past, just because the string is created in a similar manner to how you're write out the items in an array doesn't mean they'll automatically convert into array items for you.

    http://php.net/manual/en/language.types.array.php

    Hope that helps.

  3. f00bar
    Member
    Posted 4 years ago #

    Ah I get it! That's me being a bit of an idiot really..thank you for enlightening me!

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    You're welcome.. ;)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags