Ticket #242 (closed enhancement: fixed)

Opened 9 months ago

Last modified 7 months ago

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

search_offset.patch (1.2 kB) - added by michaeltwofish 9 months ago.

Change History

Changed 9 months ago by michaeltwofish

Changed 8 months ago by michaeltwofish

  • owner set to michaeltwofish
  • status changed from new to assigned

There are some flaws in the patch attached. After discussion with ringmaster I have another approach planned.

Changed 7 months ago by michaeltwofish

  • status changed from assigned to closed
  • resolution set to fixed

Alternate approach committed in r1592. More work is required (allowing arbitrary offsets, more sensible retrieval when not using the default).

Note: See TracTickets for help on using tickets.