General
Getting & Giving Support
Administration
Design
- Tut. - Layouts and Themes
- Tut. - Navigation cookbook
- Tut. - How To section
Reference Material
Plugin Development
- Tut. - Writing a plugin
- Ref. - List of Events
- Ref. - Enabling cron jobs
- Ref. - PHPDoc
Core Development
- Ref. - PHPDoc
Press information
Wolf CMS books section
linkById()
This function1) makes it possible to create a “persistent link” to a page, so that the link will not break even if the page is moved to a different location in the page hierarchy. It has the same options as the link function (see that page for explanations and examples). The page ID is used as the basis for the link, rather than the page's “slug”.2)
See also: urlById()
Usage
<?php echo Page::linkById(3); ?>
or
<?php echo $this->linkById(3); ?>
where “3” in the example above is the “id” of the targetted page.
To find out what the ID of a given page is, either look on the “metadata” tab, or hover above the page's title or icon in the main admin page listing, where the ID will appear as a tooltip.
Using a variable for the ID
Using a simple variable for the ID may not pass the filter test set by the linkById() function, since the variable might be a “string” rather than an “integer”, which is what this function requires. In other words, something like this:
<?php echo Page::linkById($article->id()); ?>
might throw an error. It is possible to set the ID number dynamically, but it might require an extra step, wrapping the variable for the ID in with the PHP intval() function, like this:
$articleId = intval($article->id()); echo Page::linkById($articleId);
Another more compact solution uses PHP (int) to cast the value as an integer:
echo Page::linkById((int)$article->id());
With either of these solutions in place, the linkById() function will work as expected.
