Jump to content

Limit button prev and next


tooth-paste
 Share

Recommended Posts

On the bottom of each project detailpage I would like to create a button "Previous" and "Next" with the limit of two images. This is what I got so far but struggling with the foreach and limit of two. I have got an image included.

<div class="row">
		
	<?php
	$project = $pages->get('/projecten/')->children("limit=2");
	$prev = $page->prev($project)->id ? $page->prev($project) : $project->last();
	$next = $page->next($project)->id ? $page->next($project) : $project->first();
					
	foreach($prev as $p) {
	echo "<div class='medium-3 small-12 columns'>
	<a href='{$prev->url}'>
	<h4>$prev->title<br />$prev->diensten</h4>
	<img src='{$prev->overzichtsthumb->first()->url}' alt='' />
	</a>
	</div>";
	}
					
	foreach($next as $n) {
	echo "<div class='medium-3 small-12 columns'>
	<a href='{$next->url}'>
	<h4>$next->title<br />$next->diensten</h4>
	<img src='{$next->overzichtsthumb->first()->url}' alt='' />
	</a>
	</div>";
	}
	?>
</div>
 

post-2727-0-94089200-1457214516_thumb.jp

Link to comment
Share on other sites

// maybe ?

$projects = $pages->get("/projecten/")->children("sort=sort");
$prev2 = 0 < $page->prev($projects)->prev()->id ? $page->prev($projects)->prev() : $page->first($projects);
$prev1 = 0 < $page->prev($projects)->id ? $page->prev($projects) : $page->first($projects);
$next1 = 0 < $page->next($projects)->id ? $page->next($projects) : $page->last($projects);
$next2 = 0 < $page->next($projects)->next()->id ? $page->next($projects)->next() : $page->last($projects);

// or, if the above do not work as expected, maybe:

$prev2 = 0 < $page->prev($projects)->prev($projects)->id ? $page->prev($projects)->prev($projects) : $page->first($projects);
$prev1 = 0 < $page->prev($projects)->id ? $page->prev($projects) : $page->first($projects);
$next1 = 0 < $page->next($projects)->id ? $page->next($projects) : $page->last($projects);
$next2 = 0 < $page->next($projects)->next($projects)->id ? $page->next($projects)->next($projects) : $page->last($projects);

But what do you want to show for prev2 and prev1, if the current page is the first?

// simply use ?

$selector = "limit=2";
$prevAll = $page->prevAll($selector);
$nextAll = $page->nextAll($selector);

// and then
foreach($prevAll as $prev) { 
...
}

foreach($nextAll as $next) { 
...
}

This only will render items if there are any. Means, if the current page is the first one, you will not get previous-links. (what makes sense, as there are no ones).

Edited by horst
Link to comment
Share on other sites

Thanks Horst! I believe your right and will only show next projects, like your last example. Makes more sense. But ran (again) into a problem. I want to show the text/arrow 'More projects', but when the last project is reached the result is null. Is it possible to hide the text/arrow if the result is null? 

Link to comment
Share on other sites

From what I understand, you should test your result (variable) if is null or not then hide your arrow if null.

if ($item->id === NULL)
    echo "<h3>result is null</h3>";
else
    echo "<h3>Arrow</h3>";
Link to comment
Share on other sites

From what I understand, you should test your result (variable) if is null or not then hide your arrow if null.

if ($item->id === NULL)
    echo "<h3>result is null</h3>";
else
    echo "<h3>Arrow</h3>";

a nullpage will return 0 for the id, not NULL, so the correct way to check is just like i wrote

if($item->id) {
    // this is a page
}

or

if(!$item->id) {
    // this is a nullpage
}

see here: https://processwire.com/api/types/nullpage/

@tooth-paste

can you show us your current code? that would make it easier. in your case it would be something like

if($items->count() > 0) {
   // we found x children, so show the arrow
}
  • Like 1
Link to comment
Share on other sites

I finally got it working. Thanks Bernhard! This is the final code.

<div class="row">
	<div class="medium-12 small-12 columns">
		<?php
		$selector = "limit=4";
		$nextAll = $page->nextAll($selector);

		if($nextAll->count() > 0) {
		echo "<h3 class='detailmore'>Meer projecten →</h3>";
		}
		?>	
	</div>
</div>
<div class="row">
	<?php
	foreach($nextAll as $next) {
	echo "<div class='medium-3 small-12 columns end'>
	<a href='{$next->url}'>
	<h4>$next->title<br />$next->diensten</h4>
	<img src='{$next->overzichtsthumb->first()->url}' alt='' />
	</a></div>";			
	}
	?>	
</div>
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...