Posts: 524
Threads: 48
Joined: Mar 2011
I have a one-page site in development and would like to automate a component which I place on the page via the DynPages plugin. Said component holds the markup for the in-page sections which are output to the page via the SimplePageContent plugin.
I did some research on the forums here and found two related threads, but I have to admit that I don't grok the menu_data() function. I then thought maybe the pages.array file generated for caching might help since I would "merely" have to extract the available pages.
My current, manually updated, structure looks like the following (basically the same for each section on the page):
Code:
<div id="seite-info">
<h2><?php getPageField('info',title); ?></h2>
<?php echo get_page_by_id('info'); ?>
</div>
Can someone give me a few pointers on how to extract a page list from the pages cache in 3.1b? I think I'll need an array of slugs which I can loop through and populate the above mini template with.
Help is much appreciated.
Posts: 1,108
Threads: 70
Joined: Aug 2009
The internal caching makes all the data available into a global array $pagesArray for use on your pages.
So you can just loop through it like so:
Code:
function listPages(){
global $pagesArray;
foreach ($pagesArray as $page) {
echo '<div id="seite-info">';
echo '<h2>'.$page['title'].'</h2>';
echo get_page_by_id('info');
echo '</div>';
}
}
MIke,.
Posts: 524
Threads: 48
Joined: Mar 2011
I basically got it to work with your help, thanks so far! What I can't fix yet is that the array doesn't list the pages in the order that they appear in the navigation. So my content is tossed around the page.
Edit: print_r() for the win! I'll try and sort by [menuOrder].
Posts: 1,108
Threads: 70
Joined: Aug 2009
updated code should do it for you...
Code:
function listPages(){
global $pagesArray;
$pages= subval_sort($pagesArray,'menuOrder'); // can now take a 3rd option 'asc' or 'desc' for sorting..
foreach ($pages as $page) {
echo '<div id="seite-info">';
echo '<h2>'.$page['title'].'</h2>';
echo get_page_by_id('info');
echo '</div>';
}
}
MIke,.
Posts: 524
Threads: 48
Joined: Mar 2011
2011-08-05, 19:55:49
(This post was last modified: 2011-08-05, 20:05:06 by infos.media.)
Unfortunately, it doesn't, Mike. :/
Now there's no output at all here and an print_r($pages) outputs nothing either. Tried putting a global $menuArray; at the top as well, to no avail.
Been researching arrays, their sorting, etc. for an hour now, only slowly getting a grip on this.
Posts: 524
Threads: 48
Joined: Mar 2011
The following seems to work
Code:
<?php
function listPages(){
global $pagesArray;
$pages= subval_sort($pagesArray,'menuOrder'); // can now take a 3rd option 'asc' or 'desc' for sorting..
foreach ($pages as $page) {
echo '<div id="' . $page['slug'] . '">';
echo '<h2>' . $page['title'] . '</h2>';
echo get_page_by_id($page['slug']);
echo '</div>';
}
}
?>
I just replaced $menuArray with $pagesArray.
Posts: 1,108
Threads: 70
Joined: Aug 2009
Oops, sorry copy/pasted from some code , never check the vars...
Posts: 12
Threads: 0
Joined: Apr 2014
Hi,
Is there a way to loop like this only into pages of a certain language?
Thanks!
Posts: 346
Threads: 27
Joined: Sep 2010
2015-08-12, 02:35:07
(This post was last modified: 2015-08-12, 02:44:43 by Angryboy.)
Since the language is tied to the suffix of the slug (e.g.
index_de
), you can perhaps try parsing that:
PHP Code:
// Initialize some variables to be used in the loop
$langSuffix = '_de'; // your language
// Main loop
foreach ($pages as $page) {
// check that the language suffix in the slug
if (strpos($page['slug'], $langSuffix) !== -1) {
// Do something with this language-specific page
// ...
}
// ...
}
(Not tested yet)