Drupal 7 A simple way to implement the next article in the previous article

Source: Internet
Author: User
Tags php file php print drupal

Add a function to the template/sites/all/themes/liuzhiliang.com/template.php file

The code is as follows Copy Code

/**
* Mynodeprevnext
* Article on an article next
* $nid-Node ID
* $type-node type
*/
function Mynodeprevnext ($nid, $type)
{
$output = ' <ul> ';

    $sql _previous = "Select N.nid, N.title, Url.alias"
                  . ' from {node} as n '
                  . ' Left JOIN {url_alias} as URL '
                  . "On concat (' node/', N.nid) =url.source"
                  . "WHERE N.nid <: nid and N.type =: type"
                  . "ORDER by N.nid DESC"
                  . "LIMIT 0, 1";

$result _previous = db_query ($sql _previous, Array (': Nid ' => $nid, ': Type ' => $type));

$strings = Array ();

foreach ($result _previous as $data)
{
$data->url = ($data->alias)? $data->alias: '/node/'. $data->nid;

        $output. = ' <li> previous: '
                . ' <a href= '. $data->url. ' "title=". $data->title. ' > '
               . $data->title
               . ' </a> '
               . ' </li> ';
   }

$sql _next = "Select N.nid, N.title, Url.alias"
. "From {node} as n"
. "Left JOIN {url_alias} as URL"
. "On concat (' node/', N.nid) =url.source"
. "WHERE N.nid >: Nid and N.type =: Type"
. "ORDER by N.nid ASC"
. "LIMIT 0, 1";
$result _next = db_query ($sql _next, Array (': Nid ' => $nid, ': Type ' => $type));

$strings = Array ();

foreach ($result _next as $data)
{
$data->url = ($data->alias)? $data->alias: '/node/'. $data->nid;

$output. = ' <li> Next: '
. ' <a href= '. $data->url. ' "title=". $data->title. ' > '
. $data->title
. ' </a> '
. ' </li> ';
}

$output. = ' </ul> ';

return $output;
}

Then to/sites/all/themes/liuzhiliang.com/templates/node.tpl.php add the following code, the specific location to see their favorite, I was at the bottom of the article display, so in the back in the <div class= " Content Clearfix "<?php print $content _attributes;?>> behind

The code is as follows Copy Code

<?php if (! $teaser):?>
<div class= "Previous_next" >
<?php Print Mynodeprevnext ($node->nid, $node->type); Top and bottom articles?>
</div>
<?php endif;?>

Original from: Ah Liang blog http://liuzhiliang.com/node/63

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.