Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have several menus generated on a page using wp_list_pages. I want to add a class to the last menu item on only one instance of these.

For example, I have a menu here:

<ul id="headerlinks">
<?php wp_list_pages('title_li=&include=24,26,28,30'); ?>
</ul>

And another like so:

<ul id="sublinks">
<?php wp_list_pages('title_li=&include=4,6,8,10,12,14,20,22'); ?>
</ul>

In my functions.php file I am using the following to add classes to the first and last menu items of wp_list_pages:

// Adds classes to first and last item in list of wp_list_pages
function add_markup_pages($output) {
$output= preg_replace('/page_item/', ' first-page-item page-item', $output, 1);
$output=substr_replace($output, " last-page-item page_item", strripos($output, "page_item"), strlen("page_item"));
return $output;
}
add_filter('wp_list_pages', 'add_markup_pages');

However, I only want to have this applied to the #headerlinks menu. How can I target it using the function above?

share|improve this question

2 Answers 2

Simply add the filter before the call and remove it afterward something like this

<ul id="headerlinks"> 
<?php 
 add_filter('wp_list_pages', 'add_markup_pages'); 
wp_list_pages('title_li=&include=24,26,28,30'); 
 remove_filter('wp_list_pages', 'add_markup_pages'); 
?> </ul>
share|improve this answer
    
Aha! That works perfectly. Thanks! –  willc Sep 27 '11 at 19:34
    
Glad i could help. –  Bainternet Sep 27 '11 at 21:25

There isn't passed any ID to the filter, but you can inspect the output and get around it using debug_backtrace() to add whatever you want depending on the backtrace result:

function wpse29645_pageslist_filter( $output, $r )
{
    echo '<pre>';
    var_dump( debug_backtrace( $output ) );
    echo '<hr />';
    var_dump( $output );
    echo '<hr />';
    var_dump( $r );
    echo '</pre>';
}
add_filter( 'wp_list_pages', 'wpse29645_pageslist_filter', 10, 2 );

// Just a test case
function wpse29645_pageslist()
{
    wp_list_pages();
}
add_action( 'the_content', 'wpse29645_pageslist' );

Just dump it in your functions php (without the test case) and inspect the provided data.


EDIT: The only really important thing when playing with filters is to add the exact number of arguments that are allowed to be modified in core, when you're finally calling the filter function: 10, 2 is priority & number of args.

This way you can easily play around with the different filters. Just add a function to your functions.php file that has as arguments the arguments that you find when apply_filters was called. Then just var_dump/print_r/var_export() the arguments to see what you can alter. The rest will be done by WP - seamless process. Isn't it?


Another option: Try a custom walker if possible.

share|improve this answer
    
Thanks for the advice. I am new to filters and walkers, so this may take me a while to figure out, but I will try! –  willc Sep 27 '11 at 17:42
    
@willc N/P. See edit/update. –  kaiser Sep 27 '11 at 18:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.