0

I would like to display the next and the previous most recent node from the current node (if it does not belong to a node).

It is somewhat similar to the pager available to the book page, except here the current node as well as the links to the next/previous nodes are not in a hierarchy and do not belong to a book.

Assuming I have 4 nodes - node1,node2,node3,node4 (all belonging to a set of node types I want to use) and each published consecutively one after the other. Then if the user is on node3, he/she should see the links for node2 and node4. If currently on node2 then node1 and node3 links should show up. If they navigate to node1, then a link for node2 should be displayed and so on and so forth.

Also, the next and previous nodes can only belong to a pre defined set of node types.

I am using D7 and wouldn't mind using either a custom function or a views block to implement this. Any ideas on how to go about it?

1

You can do it with db_query in your node--content_type.tpl.php. Get all nids for content type in array. Match the current nid position in array. Like if Position is array[3] the Previous nid link should be array[2] and next should be array[4].

  • hey @sumit, I tried doing this by writing a generic function. Please do take a look and tell me if it's alright. It does get the job done, but my PHP isn't great and I could use some pointing in the right direction if the code could be improved upon. – bcosynot Dec 6 '12 at 15:40
  • @vr3690 sry but i also don't have so much of experience...Its up to you if the function filled your requirement – Sumit Madan Dec 6 '12 at 16:35
1

You probably want to check the 3rd party contributed module called Previous/Next API

Description

This module allows you to know the previous or next nodes for any given node. This is very useful for providing navigational links to the user without the expensive queries required to dynamically deduce such information on the fly.

Scalability

Although the previous and next nodes can be deduced with some SQL work, the queries to do so are very heavy on the database, and can bring a site to its knees. This module solves this problem by storing the previous/next node in a table so lookups are fast. Once the module is installed, it will build this index backwards via cron until all nodes have been indexed. See the "More Info" section below for a detailed post on the positive scalability impacts of implementing this module.

Configuration

The module can be restricted to certain content types to be included in the previous/next indexing. For example, you want the site's visitors to browse through video and image nodes only, but not blogs and regular pages.

The number of nodes to index is defined in the settings too. The default is 200, but you may want to lower that for a site on shared hosts. Once the indexing is complete for all the site's nodes, cron will do do anything. You can always reindex the site using the "Re-Index" button on the settings page.

  • This could work too. I am probably going to modify my function to implement a similar features. But won't be using this as I need some further customisation. Thanks for the link though. – bcosynot Dec 7 '12 at 15:53
0

I tried doing this using Views, but wasn't able to, so I wrote a function to achieve it. If someone has a better method of accomplishing the same, please do let me know. I am not that great with PHP, so if something can be optimised or needs fixing, I am all ears.

function pager_next_prev($nid,$type,$created,$nextOrPrevious)
{
    $rv=''; //return value
    //I don't want to show this pager on 'book' or 'page' content type
    if($type=='book' || $type=='page') 
    {
        $rv='';
    }
    else
    {
        //here we check if the current node belongs to a book
        $doPrint=false;
        $queryForBID="SELECT b.bid from {book} b where nid=:nid";
        $result=db_query($queryForBID,array(':nid'=>$nid))->fetchField();
        $bid=0;
        if(null!=$result)
        {
            if($result!=0 || $result>0 || $result!='')
                $bid=$result;
        }
        if($bid!=0)
        {
            //if it does, then we return empty string
            $rv='';
        }
        else
        {
            //here we declare a set of valid content types using the machine names of the same
            $validTypes=array('set','of','valid','content','types');
            $queryForTitle="";
            $prepend='';
            //change query if we need the next or the previous most recent node
            if($nextOrPrevious=='next')
        {
            $queryForTitle="SELECT n.title title,n.nid nid FROM {node} n WHERE n.type IN (:types) AND n.created>:created LIMIT 1";
            $prepend='<<';
        }
        else
        {
            $queryForTitle="SELECT n.title title,n.nid nid FROM {node} n WHERE n.type IN (:types) AND n.created<:created ORDER BY n.created DESC LIMIT 1";
            $prepend='>>';
        }
        //fetch title and nid from DB
        $result=db_query($queryForTitle,array(':types'=>$validTypes,':created'=>$created));
                    $title='';
        if ($result)
        {
            while ($row = $result->fetchAssoc()) 
            {
                $title=$row['title'];
                $adjacentNid=$row['nid'];
            }
        }
            if(null!=$result)
            {
                //if no results found, return empty string
                if($title!='')
                {
                    //if some node is found, find it's url 
                    $queryForURL="SELECT p.alias FROM {url_alias} p WHERE p.source=:source LIMIT 1";
                    $url=db_query($queryForURL,array(':source'=>'node/'.$adjacentNid))->fetchField();
                    //set return value
                    if($nextOrPrevious=='next')
                    {
                        $rv='<a href="http://www.whackk.com/'.$url.'">'.$prepend.$title.'</a>';
                    }
                    else
                    {
                        $rv='<a href="http://www.whackk.com/'.$url.'">'.$title.$prepend.'</a>';
                    }
                }
                else
                {
                    $rv='';
                }
            }
            else
            {
                $rv='';
            }
        }
    }
    return $rv;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.