Does anyone know of a hack/patch to allow Drupal to display links for the next/previous blog entries from a user? I'm looking into how to implement such a feature, but I'd rather not re-invent the wheel if I don't have to.

Thanks,

--R.J.
http://www.electric-escape.net/

Comments

rjung’s picture

Okay, for any other folks who are interested in this functionality, this code will do the trick. Just stick the function into blog.module and you're good to go:

/* Insert previous/next blog entry links for a blog view --RJung 7/2005 */
function blog_nodeapi(&$node, $op, $teaser = NULL, $page = NULL)
{
  if (($op == 'view') && ($node->type == 'blog') && ($teaser == FALSE) && ($page == TRUE))
  {
    /* Get link to previous blog entry, if any */
    $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.type = 'blog' AND n.uid = %d AND n.status = 1 AND n.created < %d ORDER BY n.created DESC"), $node->uid, $node->created, 0, 1);
    $prevlink = 'None';
    while ($rnode = db_fetch_object($result))
    {
      $prevlink = l($rnode->title, 'node/' . $rnode->nid);
    }

    /* Get link to next blog entry, if any */
    $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.type = 'blog' AND n.uid = %d AND n.status = 1 AND n.created > %d ORDER BY n.created ASC"), $node->uid, $node->created, 0, 1);
    $nextlink = 'None';
    while ($rnode = db_fetch_object($result))
    {
      $nextlink = l($rnode->title, 'node/' . $rnode->nid);
    }

    /* Inject links before the node body */
    $node->body = '<div class="nav">' . '<p><i>Previous:</i> ' . $prevlink . '<br /><i>Next:</i> ' . $nextlink . '</p></div>' . $node->body;
  }
}

--R.J.
http://www.electric-escape.net/

--R.J.

somes’s picture

cheers R.J for the code looks similar to that in the book module

do you know what

$result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n WHERE n.type = 'blog' AND n.uid = %d AND n.status = 1 AND n.created < %d ORDER BY n.created DESC"), $node->uid, $node->created, 0, 1);

does or shall i say the ,0,1) on the end

I'll play around with it tomorrow after work have tried subing in

$query = "SELECT n.nid, n.created FROM {node} n INNER JOIN term_node ON n.nid = term_node.nid WHERE n.type = '$node_type' AND term_node.tid = $taxo_id AND n.status = 1 ORDER BY n.created ASC";
$results1 = db_query_range(db_rewrite_sql($query), $instancePos, 1);

but not having any luck listing a blog

rjung’s picture

The ", 0, 1)" at the end of the db_query_range() means you just want to fetch the first result from the query. The query already returns results earlier than the date given, sorted in descending order ("n.created < %d ORDER BY n.created DESC"), and the "0, 1" limits it to one previous entry by timestamp. So the call will fetch just one entry that's earlier than the date of the current entry -- which means the previous (by date) node.

You can get documentation for db_query_range() here.

As for your code snippet not working, perhaps the single-quotes around "$node_type" are preventing it from being substituted. Maybe you can try using string concatenation instead:

$query = "SELECT n.nid, n.created FROM {node} n INNER JOIN term_node ON n.nid = term_node.nid WHERE n.type = \'" . $node_type . "\' AND term_node.tid = " . $taxo_id . " AND n.status = 1 ORDER BY n.created ASC";

I'm not a SQL expert, but it's worth a quick test.

--R.J.
http://www.electric-escape.net/

--R.J.

spiffyd’s picture

Does this work for 6.x?

Drupal core needs to have this simple but necessary function for blog nodes!

spiffyd’s picture

spiffyd’s picture