• I’m trying to show child-pages in a certain order, in a list.

    This is my code to output the pages as a list

    $parent = $wp_query->post->ID;
    		if(wp_list_pages("title_li=&child_of=$parent&echo=0" )) {
    			wp_list_pages("title_li=&child_of=$parent&echo=1" );
    }

    Now I want to show the first four of the pages in a particular order

    -Summary
    -Info
    -Groups
    -Entries
    -Other Page
    -Another Page

    In other words, Summary, Info, Groups, Entries should be shown in that order (providing they exist), and other pages below. How would I achieve that?

Viewing 3 replies - 1 through 3 (of 3 total)
  • This should do it:

    $titles_to_match = array('Summary','Info','Groups','Entries');
    $titles_regex = '/' . implode('|',$titles_to_match) . '/';
    $pagelist = wp_list_pages("title_li=&child_of=$parent&echo=0");
    $pagearray = explode('<li ',$pagelist);
    $junk = array_shift($pagearray); // First entry is empty
    for ($i=0;$i<sizeof($pagearray);++$i) {
      if (preg_match($titles_regex,$pagearray[$i],$matches)) {
        $special[$matches[0]] = '<li ' . $pagearray[$i];
      } else {
        $newarray[] = $pagearray[$i];
      }
    }
    foreach($titles_to_match as $title) {
      $newlist .= $special[$title];
    }
    $newlist .= '<li ' . implode('<li ',$newarray);
    echo $newlist;

    Thread Starter cannyboy

    (@cannyboy)

    Thanks. That spits out the menus, but not in the order required. I used a couple of echo statements to try and work out what is going on and the output is:

    pregmatch else else
    Entries
    Groups & Guides
    Info & Misc

    Here’s the code I used

    $parent = $wp_query->post->ID;
    
    $titles_to_match = array("Info & Misc","Groups & Guides","Entries");
    $titles_regex = '/' . implode('|',$titles_to_match) . '/';
    $pagelist = wp_list_pages("title_li=&child_of=$parent&echo=0");
    $pagearray = explode('<li ',$pagelist);
    $junk = array_shift($pagearray); // First entry is empty
    for ($i=0;$i<sizeof($pagearray);++$i) {
      if (preg_match($titles_regex,$pagearray[$i],$matches)) {
      echo "pregmatch ";
        $special[$matches[0]] = '<li ' . $pagearray[$i];
      } else {
       echo "else ";
        $newarray[] = $pagearray[$i];
      }
    }
    foreach($titles_to_match as $title) {
      $newlist .= $special[$title];
    }
    $newlist .= '<li ' . implode('<li ',$newarray);
    echo $newlist;

    Well, you didn’t give the real titles in your example, so the regex is not correct. Ampersands and other characters have a special meaning in html and are encoded when used in a title. They must be encoded before used in the regex. I think this will handle all of the special characters, but no guarantees – best to avoid them in titles.

    $titles_to_match = array("Info & Misc","Groups & Guides","Entries");
    for ($i=0;$i<sizeof($titles_to_match);++$i) {
      $titles_to_match[$i] = htmlspecialchars($titles_to_match[$i]);
    }
    $titles_regex = '/' . implode('|',$titles_to_match) . '/';
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Listing pages in a certain order’ is closed to new replies.