WordPress Page Pagination IF query

<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
   $pages[] += $page->ID;
}

$current = array_search(get_the_ID(), $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>

<div id="page_nav">
	<div class="wrapper">
		<a href="<?php echo get_permalink($prevID); ?>"><i class="fa fa-arrow-left"></i>Previous Work</a><span>/</span>
		<a id="back_link" href="http://kyleskelly.co.uk/#projects_container">Back to Projects</a><span>/</span>
		<a href="<?php echo get_permalink($nextID); ?>">Next Work<i class="fa fa-arrow-right"></i></a>
	</div>
</div>

My knowledge of php isn’t great, so any help would be appreciated, thanks!

Read More

I want my code to query –

IF is first page in portfolio then the a tag for prev link is inactive.

else if last page in portfolio then next page link is inactive.

else the code i already have

Related posts

Leave a Reply

1 comment

  1. I haven’t tried this code, but I think it should work. Please let me know if something arises. I’ve commented the code to explain you more or less what I’ve done.

    <?php
    $pagelist = get_pages('sort_column=menu_order&sort_order=asc');
    $current = get_the_ID();// Current Page ID. I assume IDs are unique, and all pages have one. All the IDs are in $pagelist.
    
    $next = $prev = FALSE;
    reset($pagelist);// Pointer to first element.
    while ($page = current($pagelist) ){// Loop through the array. I used while instead of foreach for its convenience to get next element.
        if( $page->ID == $current) {
            $obj = next($pagelist);
            $next = $obj ? $obj->ID : FALSE;// If there's a next, get the ID, otherwise we are the last page.
            break;
       }
    
       $prev = $page->ID;// Previous element
       next($pagelist);// Update pointer
    }
    
    ?>
    
    <div id="page_nav">
        <div class="wrapper">
            <?php if($prev !== FALSE) {// If I have a previous element, print out a ?>
            <a href="<?php echo get_permalink($prev); ?>"><i class="fa fa-arrow-left"></i>Previous Work</a><span>/</span>
            <?php } ?>
            <a id="back_link" href="http://kyleskelly.co.uk/#projects_container">Back to Projects</a><span>/</span>
            <?php if($next !== FALSE) {// If I have a next element, print out a ?>
            <a href="<?php echo get_permalink($next); ?>">Next Work<i class="fa fa-arrow-right"></i></a>
            <?php } ?>
        </div>
    </div>