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 am using the WP-Paginate plug-in and need to apply a class to the containing LIs for the next/previous links. Can someone tell me how to do this? If there's a better/easier way to do this other than using WP-Paginate, then I'm open to suggestions. Thanks!

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Hi @user1462:

Happy to say that it's not hard at all to achieve what you are looking for.

As I understand it, you have to add a call to wp_paginate() in (one of) your theme(s) template file(s) when using this plugin, and that call looks something like this:

<?php if(function_exists('wp_paginate')) {
  wp_paginate();
} ?>

To do what you need just create your own function, let's call it mysite_paginate() and call it instead of the code above.

Within your function mysite_paginate() call ob_start() to start output buffering, then call wp_paginate(), and then call ob_get_clean() to capture the output created by wp_paginate(). The final step is to then use a regular expression search and replace with preg_replace() to add the classes you want and then echo the results to the browser.

The code you need is below. You can add it to your theme's functions.php file or in a .php file of a plugin you may be writing:

function mysite_paginate() {
  if(function_exists('wp_paginate')) {
    ob_start();
    wp_paginate();
    $html = ob_get_clean();
    $html = preg_replace('#<li>(<a href="[^"]+" class="next">)#Us',
              '<li class="next">$1',$html);
    $html = preg_replace('#<li>(<a href="[^"]+" class="prev">)#Us',
              '<li class="prev">$1',$html);
    echo $html;
  }
}

And if all goes as planned, this is what you should expect to see when using an element inspector with your browser:

Screenshot showing classes added to the HTML output of WordPress

share|improve this answer
    
Nice post, Mike. I was trying to avoid suggesting anything too "advanced", not knowing the OP's comfort level with WP, but this certainly would do the trick ;) –  Eric Martin Jan 22 '11 at 22:32
    
@Eric Martin - Thanks. The site is as much about creating a persistent resource that can be found by Google for people with the same question in the future as it is about meeting that persons needs so I try to be thorough. Also, check this out: meta.wordpress.stackexchange.com/questions/266 Maybe you could encourage questions about your plugins here? –  MikeSchinkel Jan 22 '11 at 23:15
    
I will definitely be adding this site as a place to get help for my plugins. I've done the same for my jQuery plugin & stackoverflow! They are a great resource. –  Eric Martin Jan 23 '11 at 15:44
    
@Eric Martin - Just to make sure it was clear, I was suggesting you encourage people to ask questions about your plugins here; it will give your plugins more exposure than having the questions be on your site or even on WordPress.org. I'm unclear if I got that across. BTW, I had not heard of your WP-Paginate plugin before but after looking at it I really, really like it. I wrote something similar custom for a client 2 years ago, but wish yours had been available at the time. –  MikeSchinkel Jan 24 '11 at 0:40
    
@MikeSchinkel - yup, sorry if my response wasn't clear. I'll be adding this site as a place to get help for my plugins ;) –  Eric Martin Jan 24 '11 at 22:31
show 1 more comment

If I understand you correctly, you want to change:

<li><a href="http://www.ericmmartin.com/blog/page/2/" class="next">»</a></li>

To:

<li class="next"><a href="http://www.ericmmartin.com/blog/page/2/" class="next">»</a></li>

Besides modifying the source, you could do it with a little jQuery magic:

$('ol.wp-paginate .next').parent().addClass('next');

And:

$('ol.wp-paginate .prev').parent().addClass('prev');

share|improve this answer
    
Unfortunately, I'm not using jQuery. :( Also, using JavaScript to do this could cause the elements to shift once the JavaScript loads. –  user1462 Jan 21 '11 at 19:41
    
I guess the only other reasonable option would be to modify the WP-Paginate code. –  Eric Martin Jan 22 '11 at 0:26
    
Great to have you here at WordPress Answers. Hope we'll see you around some more. –  MikeSchinkel Jan 22 '11 at 8:09
add comment

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.