Hardcoded Prev/Next, getting ExternalLink() error
Permalink
Hi, I hardcoded the previous and next page link block into one of my templates, based on this:http://concrete5tricks.com/blog/hardcode-previous-and-next-page-lin... ...
And I'm getting this error on my Page Types > Defaults page:
Any idea what this is or how I can fix this issue?
Thanks for any help!
<?php //initialize the block $next_prev = BlockType::getByHandle('next_previous'); //set your options $next_prev->controller->orderBy = 'display_asc'; $next_prev->controller->loopSequence = true; $next_prev->controller->excludeSystemPages = true; //get the pages $prev_page = $next_prev->controller->getPreviousCollection(); $next_page = $next_prev->controller->getNextCollection(); //get url's to the pages $nh = Loader::helper('navigation'); $prev_url = $nh->getLinkToCollection($prev_page); $next_url = $nh->getLinkToCollection($next_page); ?>
Viewing 15 lines of 17 lines. View entire code block.
And I'm getting this error on my Page Types > Defaults page:
Fatal error: Call to a member function isExternalLink() on a non-object in C:\xampp\htdocs\site\concrete\core\helpers\navigation.php on line 36
Any idea what this is or how I can fix this issue?
Thanks for any help!
Maybe try verifying that $next_page and $prev_page are actually set before generating the links. Like this:
Thanks for the quick response! Unfortunately, I get the same error.
Hmmm. Could you repost your code with the is_object() checks I suggested?
Sure! Here it is:
<?php //initialize the block $next_prev = BlockType::getByHandle('next_previous'); //set your options $next_prev->controller->orderBy = 'display_asc'; $next_prev->controller->loopSequence = true; $next_prev->controller->excludeSystemPages = true; //get the pages $prev_page = $next_prev->controller->getPreviousCollection(); $next_page = $next_prev->controller->getNextCollection(); //get url's to the pages $nh = Loader::helper('navigation'); $prev_url = $nh->getLinkToCollection($prev_page); $next_url = $nh->getLinkToCollection($next_page); ?>
Viewing 15 lines of 21 lines. View entire code block.
You need to take out the lines:
Because we aren't using those anymore and it's still possibly sending a null to getLinkToCollection(). So your code should look like this:
$prev_url = $nh->getLinkToCollection($prev_page); $next_url = $nh->getLinkToCollection($next_page);
Because we aren't using those anymore and it's still possibly sending a null to getLinkToCollection(). So your code should look like this:
<?php //initialize the block $next_prev = BlockType::getByHandle('next_previous'); //set your options $next_prev->controller->orderBy = 'display_asc'; $next_prev->controller->loopSequence = true; $next_prev->controller->excludeSystemPages = true; //get the pages $prev_page = $next_prev->controller->getPreviousCollection(); $next_page = $next_prev->controller->getNextCollection(); //get url's to the pages $nh = Loader::helper('navigation'); ?> <?php if(is_object($prev_page)):?> <a href="<?php echo $nh->getLinkToCollection($prev_page); ?>">Previous Page</a>
Viewing 15 lines of 19 lines. View entire code block.
IT WORKS! Thank you!!
Awesome! Glad I could help! I'll post a comment on that blog and let jordanlev know that section could cause people some problems.
Yeah, that would be helpful! Thanks again!
Sorry, when I originally posted my previous message I forgot to remove those two lines from the complete code. I updated it now so it should be good. Let me know if that works for you.