Forum

Thread tagged as: Question, Problem, Blog

Issue getting next/prev buttons working

Sorry but PHP isn't my strong suit and I've been having trouble converting the next/previous solutions article to blog posts.

I can't get my template to show up on the page and I'm not really sure what I'm doing wrong. :(

This is my php

<?php 

 perch_blog_custom(array(
  'sort'=>'postDateTime',
  'sort-order'=>'DESC',
  'value' => $result[0]['_sortvalue'],
  'template'=>'blog/post.html',
  'count'=>1,
  'skip-template' => true,
  'return-html'   => true,
  ));   

 PerchSystem::set_var('is_next', true);

 perch_blog_custom(array(
     'template'   => 'blog/post_comic_page_navigation.html',
     'filter'     => '_order',
     'match'      => 'gt',
     'value'      => $result[0]['_sortvalue'],
     'sort'       => '_order',
     'sort-order' => 'ASC',
     'count'      => 1,
 ));

 PerchSystem::set_var('is_prev', true);

 perch_blog_custom(array(
     'template'   => 'blog/post_comic_page_navigation.html',
     'filter'     => '_order',
     'match'      => 'lt',
     'value'      => $result[0]['_sortvalue'],
     'sort'       => '_order',
     'sort-order' => 'DESC',
     'count'      => 1,
 ));

 echo $result['html'];

 ?>

And this is my template...

<div class="comic-navigation">
    <ul class="comic-navigation__main-nav">
        <perch:if exists="is_prev"><li><a class="comic-navigation__previous" href="?s=<perch:content id="slug" type="slug" />">Previous</a> </li></perch:if>
        <perch:if exists="is_next"><li><a class="comic-navigation__next" href="?s=<perch:content id="slug" type="slug" />">Next</a></li></perch:if>
    </ul>           

    <p class="comic-navigation__secondary-nav"><a href="">First</a> | Page 30 | <a href="">Latest</a></p>
</div>
Jana Hoffmann

Jana Hoffmann 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You're not populating $result anywhere. This:

perch_blog_custom(array(
  'sort'=>'postDateTime',
  'sort-order'=>'DESC',
  'value' => $result[0]['_sortvalue'],
  'template'=>'blog/post.html',
  'count'=>1,
  'skip-template' => true,
  'return-html'   => true,
  ));   

should be:

$result = perch_blog_custom(array(
  'sort'=>'postDateTime',
  'sort-order'=>'DESC',
  'value' => $result[0]['_sortvalue'],
  'template'=>'blog/post.html',
  'count'=>1,
  'skip-template' => true,
  'return-html'   => true,
  ));   

Thanks Drew, that fixed them not displaying. Unfortunately I'm still having trouble getting it to work the way I want.

Currently my previous button is displaying the first post, instead of the actual previous post and my next button is showing the most recent, instead of the next one. :S

My new php...

<?php 

$result = perch_blog_custom(array(
    'sort'=>'postDateTime',
    'sort-order'=>'ASC',
    'value' => $result[0]['_sortvalue'],
    'template'=>'blog/post_comic_page_prev.html',
    'count'=>1,
    'skip-template' => true,
    'return-html'   => true,
    )); 

PerchSystem::set_var('is_prev', true);

$result = perch_blog_custom(array(
    'template'   => 'blog/post_comic_page_prev.html',
    'filter'     => '_order',
    'match'      => 'gt',
    'value'      => $result[0]['_sortvalue'],
    'sort'       => 'postDateTime',
    'sort-order' => 'ASC',
    'count'      => 1,
    ));

echo $result['html'];

?>

<?php 

$result = perch_blog_custom(array(
    'sort'=>'postDateTime',
    'sort-order'=>'DESC',
    'value' => $result[0]['_sortvalue'],
    'template'=>'blog/post_comic_page_next.html',
    'count'=>1,
    'skip-template' => true,
    'return-html'   => true,
    ));

PerchSystem::set_var('is_next', true);

$result = perch_blog_custom(array(
    'template'   => 'blog/post_comic_page_next.html',
    'filter'     => '_order',
    'match'      => 'lt',
    'value'      => $result[0]['_sortvalue'],
    'sort'       => 'postDateTime',
    'sort-order' => 'DESC',
    'count'      => 1,
    ));

echo $result['html'];

?>

Don't think it's important but just incase my post_comic_page_prev.html...

<perch:if exists="is_prev"><li><a class="comic-navigation__previous" href="/blog/post.php?s=<perch:blog id="postSlug" type="slug" />">Previous</a> </li></perch:if>

and the post_comic_page_next.html

<perch:if exists="is_next"><li><a class="comic-navigation__next" href="/blog/post.php?s=<perch:blog id="postSlug" type="slug" />">Next</a></li></perch:if>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Change:

'filter'     => '_order',

to

'filter'     => 'postDateTime',

And change:

'value'      => $result[0]['_sortvalue'],

to

'value'      => $result[0]['postDateTime'],

Woo thanks Drew!

Sorry I thought it was fixed and marked it as solved but it's still not quite working so I started another thread

https://forum.grabaperch.com/forum/01-10-2015-nextprev-buttons-still-not-working-correctly