• In my theme I’m using the wp_list_pages(‘title_li=’) for my site menu. I want to append a static query string to these A tag URL’s. Seems not possible to do using this function? Should I use another?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The sample code below shows how to add a new argument to each link from wp_list_pages:

    <?php
     function mam_add_query_arg ($key,$value,$link) {
       // Adds the parameter $key=$value to $link, or replaces it if already there.
       // Necessary because add_query_arg fails on wp_list_pages entries.
       if (strpos($link,'href')) {
         $hrefpat = '/(href *= *([\"\']?)([^\"\' ]+)\2)/';
       } else {
         $hrefpat = '/(([\"\']?)(http([^\"\' ]+))\2)/';
       }
       if (preg_match($hrefpat,$link,$matches)) {
          $url = $matches[3];
          $newurl = add_query_arg($key,$value,$url);
          // echo '<p>OLDURL:' . htmlspecialchars($url) . '</p>';
          // echo '<p>NEWURL:' . htmlspecialchars($newurl) . '</p>';
          $link = str_replace($url,$newurl,$link);
       }
    
       return $link;
    }
    
    $pagelist = wp_list_pages('title_li=&child_of=327&echo=0');
    echo '<p>Before Loop:';print_r(htmlentities($pagelist));echo '</p>';
    $pagearray = explode('<li ',$pagelist);
    $junk = array_shift($pagearray); // First entry is empty
    for ($i=0;$i<sizeof($pagearray);++$i) {
      $newarray[$i] = mam_add_query_arg('newkey','newval',$pagearray[$i]);
      echo '<p>i='.$i.'  NewLINK=' . htmlentities($newarray[$i]) . '</p>';
    }
    $newlist = '<li ' . implode('<li ',$newarray);
    echo '<p>After Loop:';print_r(htmlentities($newlist));echo '</p>';
    ?>
    Thread Starter cewyattjr

    (@cewyattjr)

    PERFECT. Thank you!

    You are welcome! Now, please use the dropdown at top right to mark this topic ‘Resolved’.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Possible to append a query_string to wp_list_pages( ) hyperlinks?’ is closed to new replies.