[resolved] Variables with "_" not working in foreach() loop in a theme (4 posts)

  1. curlybracket
    Posted 5 years ago #

    Within a theme I am trying to work around the exclude_tree bug, using the code of this page : http://www.technokinetics.com/workaround-for-exclude_tree-bug-in-wp_list_pages/

    I will simplify the code to get to my problem :

    <?php $parent_pages_to_exclude = array(8,233);
         foreach ($parent_page_to_exclude as $page_to_exclude) :
                echo "page_id: $page_to_exlude";

    This does not work at all, and does not show any error.

    When I am trying the same code naming the variables differently, I get the expected result :

    <?php $lols = array(8,233);
         foreach ($lols as $lol) :
             echo "lol $lol";

    What am I missing here? Are variable names expected to not contain underscores anymore?

  2. Michael
    Forum Moderator
    Posted 5 years ago #

    typo alert:
    wrong $parent_pages_to_exclude in first line


    <?php $parent_page_to_exclude = array(8,233);
         foreach ($parent_page_to_exclude as $page_to_exclude) :
                echo "page_id: $page_to_exlude";

    you must have the php warnings disabled, somehow;
    the code gave me an immediate

    Warning: Invalid argument supplied for foreach() in ..

  3. curlybracket
    Posted 5 years ago #

    indeed there is a typo. It does not resolve my problem however.

  4. curlybracket
    Posted 5 years ago #

    Ok, it had nothing to do with the variable names, neither with the foreach.
    So my simplified approach was totally useless as the problem was farther within the supplied workaround.

    Here is the modified code (for reference)

                // wp_list_pages('exclude=2&exclude_tree=8,233&sort_column=menu_order&title_li=');
                // workaround for exclude_tree bug
                // http://www.technokinetics.com/workaround-for-exclude_tree-bug-in-wp_list_pages/
                $page_exclusions = '';
                $parent_pages_to_exclude = array(8,233);
                foreach ($parent_pages_to_exclude as $parent_page_to_exclude) :
                    $page_exclusions .= "$parent_page_to_exclude,";
                    $descendants = get_pages('child_of=' . $parent_page_to_exclude);
                    foreach($descendants as $descendant) {
                        $page_exclusions .= $descendant->ID .',';
                wp_list_pages('title_li=&sort_column=menu_order&exclude=2,' . $page_exclusions);

    I had to declare $page_exclusions. i also did inverse the construction of the string (first put the variable, then the comma).

Topic Closed

This topic has been closed to new replies.

About this Topic