wordpress coding question

4 replies
I am making an option, that allows me to add the names of the pages I want to show in the footer, and here is what I need help with, basically I have a variable of allowed pages, which is pointed to the wordpress option where I list the pages, However, the foreach loop doesn't loop through each page I listed because it thinks it's all one string, I done this manually without creating an option and it work just fine, Ideally, I need an option so I don't have to edit code.

heres what it looks like, maybe you guys can help me get it straightened out

PHP Code:
<?php
  
= array(get_option('allowed_pages'));

foreach ( as ) {
  echo ();

}

?>
output: "contact us, about us" (Notice how this is outputted as a string)

thats the code that doesn't work right, I wan't it to list all the pages I got in the option, and the pages are set up in the options like 'contact us','about us','privacy policy' however the loop thinks its one string.

this is the code that worked before without the option, this looped through all the pages, and displayed them in a list, (I had them in an unordered list before).
PHP Code:
<?php
 
= array('contact us','about us','privacy policy');

foreach ( as ) {
  echo ();
  echo 
"<br/>";

}




?>
output:
contact us
about us
privacy policy

that right there works perfect but for some reason it doesn't work when I use the wordpress option.

edits: the variables are blocked, but if you know PHP you should recognize the structure
#coding #question #wordpress
  • Profile picture of the author phpbbxpert
    First this is not right
    = array(get_option('allowed_pages'));
    That line is defining an array with one string, the allow_pages value.
    Not what you want.

    You could do something like
    $page_ary = get_option('allowed_pages');
    $page_ary = explode(",", $page_ary);

    Now $page_ary is an array.

    But why not just define a new navigation and use WP 3.0's menu system?
    {{ DiscussionBoard.errors[2851588].message }}
  • Profile picture of the author scott33
    you might of not see me say the variables was being blocked out, thats what i did only $allowed_pages = array();

    but this worked.. thanks bro

    now i can loop through correctly and list all the pages in links..

    its weird that it worked when i manually put the items in, and didn't when i was just putting the option in
    {{ DiscussionBoard.errors[2851830].message }}
  • Profile picture of the author phpbbxpert
    See WP options excepts a few types, int, string, array, etc....

    So if you set an option with say
    $test = array('test1', 'test2', 'test3')

    The output of that option will be a serialized array.

    If the same option is set with
    $test = 'test1, test2, test3';
    The output of the option is going to be a string.

    AND your get_option() was most likely returning like this
    $test = array('test1, test2, test3');

    It is going to be an array with only one index which is a string.

    Which is why I used the explode function to convert the string to an array on a comma deliminator.
    {{ DiscussionBoard.errors[2851997].message }}
  • Profile picture of the author scott33
    cool this is what i was going for BTW

    <?php
    $allowed_pages = get_option('allowed_pages');
    $allowed_pages = explode(",",$allowed_pages);
    //$test = array($allowed_pages);
    foreach ($allowed_pages as $value) {
    echo "<ul>";
    echo "<li><a href='$value'>$value</a></li>";
    echo "</ul>";
    }
    ?>

    it list every page in an un ordered list and links to each page that i specify in my themes footer links option
    {{ DiscussionBoard.errors[2855688].message }}

Trending Topics