XF 2.0 Canonical for a post

Ouard

Member
Hello,

I want to get the real canonical url for a post.

Output of the next code is
".../posts/18/"
but I want to get
"../threads/abc.6/page-2#post-18"

PHP:
$app = \XF::app();
$router = $app->router('public');
$post = $app->em()->find('XF:Post', 18);
echo $router->buildLink('canonical:posts', $post);
 
You need the thread record for this.
PHP:
$app = \XF::app();
$router = $app->router('public');
$post = $app->em()->find('XF:Post', 18, 'Thread');
$thread = $post->Thread;
echo $router->buildLink('canonical:threads/posts', $thread, ['post_id' => $post->post_id]);
 
Thanks but the result is :
../threads/abc.6/posts?post_id=18
and not
../threads/abc.6/page-2#post-18
 
I made a typo, it should be:
PHP:
$router->buildLink('canonical:threads/post', $thread, ['post_id' => $post->post_id]);
It will produce:
...threads/abc.6/post-18

Note that I appreciate it's not exactly the output you want, but the link it produces will redirect to the correct page and the correct post anchor. It's the same one we use on posts. This is useful, for example, if the number of posts per page changes.
 
Top Bottom