• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 2.0 Get thread_id in widget

AndyB

Well-known member
#1
I would like to add a widget that will be used to show a block while in thread_view. I have selected the Thread view: Sidebar.

In my PHP code I'm trying to get the thread_id from the URL. It would be nice to be able to use something like this, but this doesn't work.

PHP:
<?php

namespace Andy\SimilarThreads\Widget;

use \XF\Widget\AbstractWidget;

class SimilarThreads extends AbstractWidget
{
    public function render()
    {
        $threadId = \XF::app()->filter('thread_id', 'uint');

Is there any PHP code that can get the thread_id from the URL?

Thank you.
 

Chris D

XenForo developer
Staff member
#2
In PHP itself:
PHP:
if (!isset($this->contextParams['thread']))
{
    // widget position doesn't appear to have thread context so do not display the widget
    return '';
}

$thread = $this->contextParams['thread'];
$threadId = $thread->thread_id;
As it turns out you might not need the thread ID as we actually make the entire thread entity available via the context params.

In the widget template:
HTML:
Thread ID is: {$context.thread.thread_id}
(And, again, it's the entire thread entity that's available).