Return to Snippet

Revision: 29527
at July 31, 2010 06:25 by trepmal


Initial Code
<?php
/* replace text nav with image nav */
/* match made by comparing page slugs to images in {template_directory}/images */

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

/*exclusion rules*/
//$exclude = implode(',',array('984')); //exclude page-id 984 from subnav
$exclude = implode(',',array()); //no exclusions

$toplevel = 'title_li=&depth=1&echo=0&exclude='.$exclude;
$siblings = $toplevel.'&child_of='.$post->post_parent;
if (function_exists(pause_exclude_pages)) {
 pause_exclude_pages();
 $links =  ($post->post_parent) ? wp_list_pages($siblings) : wp_list_pages($toplevel);
 resume_exclude_pages();
}
else {
 $links =  ($post->post_parent) ? wp_list_pages($siblings) : wp_list_pages($toplevel);
}

$links = array_filter(explode("</li><li",'</li>'.$links.'<li')); //split by <li>s
function addback(&$input) {
                $orig = $input;
        $pos = strpos($input, '>');
        $pos1 = strpos($input, '>',$pos+1)+1;
        $pos2 = strpos($input, '<', $pos1+1);
        $anchortext = substr($input, $pos1, $pos2-$pos1);
        $anchortext2 = '>'.$anchortext.'<';

        $pos = strpos($input, 'href="')+6;
        $pos1 = strpos($input, '"',$pos);
        $href = substr($input, $pos, $pos1-$pos-1); //-1 to remove trailing slash
        $slug = substr($href, strrpos($href,'/'));

        $img = '<img src="' . get_bloginfo('template_directory') . '/images/' . $slug . '.jpg" />';

        $newtext = $img;
                $newtext = '>'.$newtext.'<';
                $input = str_replace($anchortext2, $newtext, $input);
        $input = '<li'.$input.'</li>'; //put them back for handling
}
array_walk($links,'addback');
$links = array_merge(array(),$links); //put things into fresh array for sake of indices 

echo '<div class="the_menu"><ul>'.implode($links).'</ul></div>';

}
/* end replace text nav with image nav */

?>

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

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

Initial Title
Image replacement for wp_list_pages()

Initial Tags
wordpress

Initial Language
PHP