Wordpress Next/Previous buttons

CSS_Guy
CSS_Guy used Ask the Experts™
on
Hi,

I'm fairly new to wordpress, but I've learned a bunch since starting this project.

Currently I have next and previous buttons in the portfolio section on my site that allows the user to cycle through the pages. Problem is, I'd like to set it up so that the next/previous buttons are only for the portfolio section and sub-sections, not include every page on the site.

example: when the user is on the portfolio main page, there should be no previous button since that is the top-level page. When the user is on the last page within the portfolio section, there should not be a 'next' button since there's no more pages in that section.

site: http://5-squared.com/portfolio
<div class="port_nav">
<?php if (!empty($prevID)) { ?>
<div class="port_prev"><a href="<?php echo get_permalink($prevID); ?>">Previous</a></div>
<?php }
if (!empty($nextID)) { ?>
<div class="port_next"><a href="<?php echo get_permalink($nextID); ?>">Next</a></div>
<?php } ?>
</div><!-- port_nav -->

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
To try and get you started on some answers, this will make it only display the next and previous if the page is a child of the portfolio page. You'll need to check the id number of your portfolio page and edit that below.

But when it gets to the last page it's still going to go one more before the nex/previous is gone.

You'll need to add a check for the next or previous page id and if it's a child of the portfolio page.

How are you setting the variables you have now? $prevID and $nextID because where those are set is where you would want to check their parent id number to stop showing the next or previous if it would be outside the portfolio section.


<?php 
global $wp_query;
$parentID = $wp_query->post->post_parent;
if ($parentID == 'x') { //ADD YOUR PORTFOLIO PAGE ID # FOR x
?>
 
<div class="port_nav">
<?php if (!empty($prevID)) { ?>
<div class="port_prev"><a href="<?php echo get_permalink($prevID); ?>">Previous</a></div>
<?php }
if (!empty($nextID)) { ?>
<div class="port_next"><a href="<?php echo get_permalink($nextID); ?>">Next</a></div>
<?php } ?>
</div><!-- port_nav -->
 
<?php } ?>

Open in new window

Author

Commented:
Hi,

Thanks a lot for the code you sent over. It's pretty much there, but as you stated, there is still the 'previous' button on the first child page, and the 'next' button on the last child page of the section.

My apologies for not including the other code. Below is the code that currently sets the variables.

Do you know how to add the check so that there won't be a previous button on the first child item, and there won't be a next button on the last child item?

I really appreciate your help.
<?php
$pagelist = get_pages('sort_column=menu_order&amp;sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
   $pages[] += $page->ID;
}
 
//print_r($pages);
 
$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
 
/*
echo "Current: $current";
echo "Prev: $prevID";
echo "Next: $nextID";
*/
?>

Open in new window

Commented:
Try changing your last code posted to this:  (Remember to change the id number from 7 to the correct one for your portfolio page.)

<?php
$pagelist = get_pages('sort_column=menu_order&amp;sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
   $pages[] += $page->ID;
}
 
//print_r($pages);
 
$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
 
/*
echo "Current: $current";
echo "<br />Prev: $prevID";
echo "<br />Next: $nextID";
*/
 
global $wp_query;
$qry = "SELECT post_parent FROM $wpdb->posts WHERE ID=$prevID";
$prevParent = $wpdb->get_var($qry);
 
$qry = "SELECT post_parent FROM $wpdb->posts WHERE ID=$nextID";
$nextParent = $wpdb->get_var($qry);
 
if ($prevParent != '7') { $prevID = ''; } //CHANGE THE 7 IN THIS LINE
if ($nextParent != '7') { $nextID = ''; } //AND IN THIS LINE
 
?>

Open in new window

Author

Commented:
Works like a charm man. Thank you so much for your help. If anyone needs the full working code, I've posted it below.

Thanks again - your a lifesaver.
<?php
$pagelist = get_pages('sort_column=menu_order&amp;sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
   $pages[] += $page->ID;
}
 
//print_r($pages);
 
$current = array_search($post->ID, $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
 
/*
echo "Current: $current";
echo "<br />Prev: $prevID";
echo "<br />Next: $nextID";
*/
 
global $wp_query;
$qry = "SELECT post_parent FROM $wpdb->posts WHERE ID=$prevID";
$prevParent = $wpdb->get_var($qry);
 
$qry = "SELECT post_parent FROM $wpdb->posts WHERE ID=$nextID";
$nextParent = $wpdb->get_var($qry);
 
if ($prevParent != '10') { $prevID = ''; } //CHANGE THE 7 IN THIS LINE
if ($nextParent != '10') { $nextID = ''; } //AND IN THIS LINE
 
?>
 
<div class="port_nav">
<?php if (!empty($prevID)) { ?>
<div class="port_prev"><a href="<?php echo get_permalink($prevID); ?>">Previous</a></div>
<?php }
if (!empty($nextID)) { ?>
<div class="port_next"><a href="<?php echo get_permalink($nextID); ?>">Next</a></div>
<?php } ?>
</div><!-- port_nav -->

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial