Image replacement for wp_list_pages()


/ Published in: PHP
Save to your folder(s)

I don't recommend this, it just needed it for a particular situation. Does not work with dropdowns.


Copy this code and paste it in your HTML
  1. <?php
  2. /* replace text nav with image nav */
  3. /* match made by comparing page slugs to images in {template_directory}/images */
  4.  
  5. if ($post->post_parent || !$post->post_parent) { // in this case, I only want the subnav displayed on certain child pages - basically, show third-level on second-level
  6.  
  7. /*exclusion rules*/
  8. //$exclude = implode(',',array('984')); //exclude page-id 984 from subnav
  9. $exclude = implode(',',array()); //no exclusions
  10.  
  11. $toplevel = 'title_li=&depth=1&echo=0&exclude='.$exclude;
  12. $siblings = $toplevel.'&child_of='.$post->post_parent;
  13. if (function_exists(pause_exclude_pages)) {
  14. pause_exclude_pages();
  15. $links = ($post->post_parent) ? wp_list_pages($siblings) : wp_list_pages($toplevel);
  16. resume_exclude_pages();
  17. }
  18. else {
  19. $links = ($post->post_parent) ? wp_list_pages($siblings) : wp_list_pages($toplevel);
  20. }
  21.  
  22. $links = array_filter(explode("</li><li",'</li>'.$links.'<li')); //split by <li>s
  23. function addback(&$input) {
  24. $orig = $input;
  25. $pos = strpos($input, '>');
  26. $pos1 = strpos($input, '>',$pos+1)+1;
  27. $pos2 = strpos($input, '<', $pos1+1);
  28. $anchortext = substr($input, $pos1, $pos2-$pos1);
  29. $anchortext2 = '>'.$anchortext.'<';
  30.  
  31. $pos = strpos($input, 'href="')+6;
  32. $pos1 = strpos($input, '"',$pos);
  33. $href = substr($input, $pos, $pos1-$pos-1); //-1 to remove trailing slash
  34. $slug = substr($href, strrpos($href,'/'));
  35.  
  36. $img = '<img src="' . get_bloginfo('template_directory') . '/images/' . $slug . '.jpg" />';
  37.  
  38. $newtext = $img;
  39. $newtext = '>'.$newtext.'<';
  40. $input = str_replace($anchortext2, $newtext, $input);
  41. $input = '<li'.$input.'</li>'; //put them back for handling
  42. }
  43. array_walk($links,'addback');
  44. $links = array_merge(array(),$links); //put things into fresh array for sake of indices
  45.  
  46. echo '<div class="the_menu"><ul>'.implode($links).'</ul></div>';
  47.  
  48. }
  49. /* end replace text nav with image nav */
  50.  
  51. ?>

URL: http://trepmal.com/scripts/image-replacement-for-wp_list_pages/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.