Ticket #242 (closed enhancement: fixed)
Search and offset for Posts objects
| Reported by: | michaeltwofish | Owned by: | michaeltwofish |
|---|---|---|---|
| Priority: | minor | Milestone: | Undetermined |
| Component: | Habari Core Software | Version: | SVN |
| Keywords: | navigation, has_patch | Cc: |
Description
The attached patch provides an easy way to find the index of a Post within a Posts object, and find the Post n away from it. This means that you can now get next and previous posts from single post templates relatively easily (some form of which should probably be put into the Theme class or the ThemeHelper plugin).
For example, a horrible and quick implementation might be:
In your theme's theme.php:
public function next( $needle, $params= array() )
{
return $this->offset($needle, -1, $params);
}
public function previous( $needle, $params= array() )
{
return $this->offset($needle, 1, $params);
}
private function offset( $needle, $offset, $params= array() )
{
if ( empty( $params ) ) {
$params= array( 'content_type' => $needle->content_type, 'status' => 'published', 'nolimit' => 1 );
}
$haystack= Posts::get( $params );
return $haystack->offset($needle, $offset);
}
In your theme's entry.single.php
<?php
$previous= $theme->previous($post);
$next= $theme->next($post);
if ( $previous ) {
echo "<a href=\"{$previous->permalink}\" title=\"{$previous->slug}\">{$previous->title}</a>";
}
if ( $previous && $next ) {
echo "|";
}
if ( $next ) {
echo "<a href=\"{$next->permalink}\" title=\"{$next->slug}\">{$next->title}</a>";
}
?>
Attachments
Change History
Note: See
TracTickets for help on using
tickets.
