Blog & Next/Prev article & Language

You are here

Neobit Studio

Blog & Next/Prev article & Language

Hi:

I think next and previous article doesn't works fine. The links shows the next and previous nid, but not the next and previous nid of the same content type (blog).

And doesn't support for multi language site

I've modified "node--blog-post.tpl.php"

Before:

/* Query Blog Nodes */
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'blog_post');
$result = $query->execute();

if (isset($result['node'])) {
$blog_post_nids = array_keys($result['node']);
$blog_posts = entity_load('node', $blog_post_nids);
}

/* Generate Paginator */
$nid = $node->nid;
$first = reset($result['node']);
$last = end($result['node']);

switch($nid) {
case $first->nid: $prev = $last->nid; $next = $nid + 1; break;
case $last->nid: $prev = $nid - 1; $next = $first->nid; break;
default: $prev = $nid - 1; $next = $nid + 1; break;
}

and I've replaced with this custom code:

/* Query Blog Nodes */
$query = new EntityFieldQuery();

global $language;

$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'blog_post')
->propertyCondition('language', $language->language, '=');
$result = $query->execute();

if (isset($result['node'])) {
$blog_post_nids = array_keys($result['node']);
$blog_posts = entity_load('node', $blog_post_nids);
}

/* Generate Paginator */
$nid = $node->nid;

$first = reset($result['node']);
$last = end($result['node']);

$item= reset($result['node']);

for ($i=1; $i<=count($result['node']); $i++)
{
if ($item->nid==$nid) break;
$item = next($result['node']);

}

$prevArticle = prev($result['node']);

if ($prevArticle === FALSE)
{
$prevArticle = $item;

}

$item= reset($result['node']);

for ($i=1; $i<=count($result['node']); $i++)
{
if ($item->nid==$nid) break;
$item = next($result['node']);

}

$nextArticle = next($result['node']);

if ($nextArticle === FALSE)
$nextArticle = $item;

$prev = $prevArticle->nid;
$next = $nextArticle->nid;