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.

thanks for taking a minute to read my question. I've done some googling and I can't seem to find the answer to this question anywhere. Forgive me if it's somewhere here on stack exchange already.

I'm using this plugin to allow "< prev" and "next >" links in my set of pages. I can change the page order using the wordpress admin menu, so this is almost perfect. However, I have a couple of pages I would like to skip in the "previous" and "next" loop.

For instance - I'm using a static home page. At one point, the "next" link links over to the home page. I'd like to exclude the home page and a couple others from this loop. Is this possible? What's the best way to do this?

Thanks!

Tim

P. S. The plugin works pretty much the same way as Next/Prev post built-in functionality works. I'm assuming if there's a way to exclude posts from this loop, that it will work the same way for pages with the plugin. Thanks

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Haven't checked the plugin you mention... but I use this solution for doing an "author" navigation.

I adjusted the functions and this should probably work without the plugin (untested). You need to adjust the ID, and the conditions itself if you want more exclusions.

[edit: corrected functions, the queries were missing the menu_order condition]

notes:

1) the ID 99999 corresponds to the static page defined as Front Page

2) all pages must have a defined menu order, i believe if there are pages with 0 (zero) as menu order this won't work

//PAGE.PHP
get_next_page_nav();
get_prev_page_nav();

//FUNCTIONS.PHP
function get_next_page_nav($link="%link", $title="%title") {
    global $wpdb, $post;
    $prev = $wpdb->get_row($wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_type='page' AND post_status='publish' AND ID!='99999' AND menu_order < '".$post->menu_order."' ORDER BY menu_order DESC LIMIT 1;"));
    if($prev) {
        $title = preg_replace('/%title/',$prev->post_title, $title);
        echo preg_replace('/%link/', '<a href="'.get_permalink($prev->ID).'" rel="prev" class="next-page">'.$title.'</a> ', $link);
    }
}  

function get_prev_page_nav($link="%link", $title="%title") {
    global $wpdb, $post;
    $next = $wpdb->get_row($wpdb->prepare("SELECT ID, post_title FROM $wpdb->posts WHERE post_type='page' AND post_status='publish' AND ID!='99999'  AND menu_order > '".$post->menu_order."' ORDER BY menu_order ASC LIMIT 1;"));
    if($next) {
        $title = preg_replace('/%title/',$next->post_title, $title);
        echo preg_replace('/%link/', ' <a href="'.get_permalink($next->ID).'" rel="next" class="previous-page">'.$title.'</a>', $link);
    } 
}
share|improve this answer
    
I'm testing this out today - thanks so much for taking the time to answer this question. I'll get back to you if I have any questions - thanks! –  timshutes Apr 18 '12 at 0:09
    
can you explain what you mean by "adjust the ID" - do you mean for the ID!='99999' exclusion? Or is there something else I need to adjust? I tried this as is and it's not working quite right. The 'next page' and 'previous page' links go to the same pages (contact us and home) no matter what page I'm on –  timshutes Apr 18 '12 at 0:44
    
yep, sorry, there's a condition missing in the query AND menu_order < '".$post->menu_order."', but it's not working perfectly, i'm doing real tests and report back –  brasofilo Apr 18 '12 at 1:06
    
Tested and working. I've updated the functions and put a couple of usage notes. –  brasofilo Apr 18 '12 at 1:23
1  
lovely! very versatile solution - thanks! –  timshutes Apr 18 '12 at 4:51
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.