Pascal Posted January 19, 2012 Share Posted January 19, 2012 Hi, I am a complete beginner in coding PHP and stumbled upon this problem. This is the code: <?php $contentpages = $pages->get("/content/")->children; unset($contentpages[0]); echo $contentpages; $footersections = $pages->get("/footer/")->children; echo $footersections; $navlinks = array_merge($contentpages, $footersections); echo $navlinks; foreach($navlinks as $link) { $class = $link === $page->rootParent ? " class='on'" : ''; echo "<div class='nav-button'><a$class href='{$link->url}'>{$link->title}</a></div>"; } ?> So first I get the children of the content page and exclude the first one, which is the homepage. The first echo spits out an array of two values. Then I repeat the procedure for sections in the footer which I want to link to. The second echo shows me those values. But then, after using array_merge to merge(?) the arrays, the resulting variable is empty. I suppose I made a really stupid mistake somewere along the way? By the way, let me know if anything more elegant comes to your mind, I am eager to learn. Link to comment Share on other sites More sharing options...
Pete Posted January 19, 2012 Share Posted January 19, 2012 There's a very good and elegant example in the default head.inc file that does exactly what you're after - well worth a look Link to comment Share on other sites More sharing options...
Adam Kiss Posted January 19, 2012 Share Posted January 19, 2012 There are also some array manipulation functions included you might want to try this: $navigation = $pages->get('/content/')->children(); $navigation->append( $pages->get('/footer/')->children()); Link to comment Share on other sites More sharing options...
Pascal Posted January 21, 2012 Author Share Posted January 21, 2012 Thanks for your replies, I am going to look into both! Link to comment Share on other sites More sharing options...
Soma Posted January 21, 2012 Share Posted January 21, 2012 You can also use an import method of PageArray. Note that the page array of PW isn't a regular php array, that's why it doesn't work. you could also do something like this. $pa = new PageArray(); $pa->import( $pages->get("/content/")->children() ); $pa->import( $pages->get("/footer/")->children() ); foreach($pa as $p){ ... } You may check out the cheatsheet on the API pages, and look for the PageArray/WireArray sections. There's lots of useful methods. For example instead of this: $contentpages = $pages->get("/content/")->children; unset($contentpages[0]); do this to slice off the first entry in the resulting page array: $contenpages = $page->get("/content/")->children()->slice(0); 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now