Forums

[resolved] 404 & wrong datatype for array (5 posts)

  1. nathan12343
    Member
    Posted 3 years ago #

    I've got a function that finds out if the current page is a sub page of another. Lifted the code from a post here I think:

    <?php // detect the page tree and set the stylesheet accordingly
    function is_tree($p_id) {
    	global $post;
    	if(is_page()&&(in_array($p_id, $post->ancestors) ||
    	   $post->post_parent==$p_id ||
    	   is_page($p_id)))
    	return true;
    	else return false;
    };
    ?>

    This lets me add a style sheet according the the top level parent. Anyway. When testing the 404 page the header goes wrong:

    Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/fhlinux133/i/inq-chat.duttonsdev.co.uk/user/htdocs/inq_cms/wp-content/themes/chat_combined/desk_header.php on line 39

    with line 39 being the 3rd line of code above.

    This error is repeated 8 times.

    Any ideas?

  2. s_ha_dum
    Member
    Posted 3 years ago #

    That's probably your call to 'in_array'. That's the only function with two arguments, and the error fits. 'in_array' needs $post->ancestors to be an array. If $post->ancestors isn't set or isn't an array you'll get that error. So you want to make sure that 'is_array' doesn't run unless $post->ancestors is an array. Something like:

    if(is_page()&&(is_array($post->acncestors) && in_array($p_id, $post->ancestors) ||

    Casting might work too.

    if(is_page()&&(in_array($p_id, (array)$post->ancestors) ||
  3. nathan12343
    Member
    Posted 3 years ago #

    Perfect!

    The first one didn;t work. Just came back with a blank page, but the second did the trick exactly!!!

    Many thanks

  4. s_ha_dum
    Member
    Posted 3 years ago #

    oops... well I did misspell 'ancestors' in the first one...

  5. nathan12343
    Member
    Posted 3 years ago #

    didn't like to mention it after the help ;)

Topic Closed

This topic has been closed to new replies.

About this Topic