CSS_Guy
asked on
Wordpress Next/Previous buttons
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
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 -->
ASKER
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.
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&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";
*/
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Thanks again - your a lifesaver.
<?php
$pagelist = get_pages('sort_column=menu_order&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 -->
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.
Open in new window