Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
882 views
in Plugins by
Currently there is no way to display a widget exclusively on Homepage. The closest option is to display on "recent questions and answers" pages, and Homepage is one of them.

Is there any PHP trick to display a widget only on Homepage, but not on any single other page?
Q2A version: 1.8.3
by
Why not? While adding the plugin, you can choose the pages to display it right? And this includes even the custom pages.
by
"recent questions and answers" is the homepage, what specifically is the problem you're having?
by
Hello Scott, the option "recent questions and answers" is not always the homepage, because there are "recent questions and answers" pages in categories. Please help us with this!

2 Answers

+2 votes
by

Hi Hella,

It works in that way because they share the same page template: 'qa'. I can also reproduce this behavior in the following pages:

  • 'Recent questions and answers', which also includes the pages shown after clicking on category links in this page
  • 'Question lists', and pages shown after clicking on category links
  • 'Recent activity', and pages shown after clicking on category links
  • 'Ask a question', which is rendered differently depending on whether the 'Ask a Question' button is clicked while visiting a category listing or not

Now, this answer will work for you if:

  • You don't mind modifying Q2A in and of itself (or using an plugin override)
  • It's possible to tweak the widget's  allow_template method

First, add the following code:

global $qa_is_category_page;
$qa_is_category_page = !empty($categoryids);

to function 'qa_content_prepare', in qa-include/app/page.php right after line 476.

Second, use this variable in the method allow_template. Let's say the widget that needs to be shown on homepage is the Category widget. Its method allow_template looks like this:

public function allow_template($template)
{
    return true;
}

but now the variable '$qa_is_category_page' is available in the function above and it's possible to check if the page being rendered is a category page or not:

public function allow_template($template)
{
    // return true;
    global $qa_is_category_page;

    return $template == 'categories' || !$qa_is_category_page;
}

You can make other widget available on homepage (but not in category pages) by following similar steps.

I hope this answer does the trick :-)

+1 vote
by

Thanks jair for the idea.

Here's how I did:

You can modify the output_widget function

if (empty($categoryids))

Your main widget content here;

}

Alternately, the plain PHP trick

if ($_SERVER['REQUEST_URI'] == "/")

Your main widget content here;

}

by
You're welcome! Thanks for sharing!

> You can modify the output_widget function
Good idea! But HTML wrapper tags will be still printed as an empty box (depending on the site theme). You can get an idea of it if you fire up the browser's Page Inspector in this page, pick the 'Related questions' widget with the Node Picker and then delete '<div class="qa-related-qs">...</div>'.
...