Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.

How can i display the 2-3 latest questions inside each category in categories list page ??

+6 votes
158 views
asked Jul 23, 2017 in Themes by butterfly
Hi. I am trying to display 2-3 last questions of each category at the categories list.

eg:

Computers - (52 Questions) - 1st question title, 2nd Question title

Tablets - (10 Questions) - 1st question title, 2nd Question title

ETC

Any idea from @Scott @sama55  ???

The ideal would be to add a function inside my advanced theme file but it also ok for me if i can add that function inside the categories List core file as well

Thanks in advance for your help guys .
Q2A version: 1.7.4
commented Jul 25, 2017 by butterfly
moved Jul 28, 2017 by Scott
Any help as it really drives me crazy ??
commented Jul 25, 2017 by sama55
It will not be difficult to add this feature. However, if there are many categories (50, 100, 200 ...), category page performance will be worse with simple logic.
commented Jul 28, 2017 by butterfly
@sama55 i have only 12 categories so i guess it wouldn't be a problem for site performance. Could you guide me please ? Thanks in advance.

1 Answer

+1 vote
answered Jul 28, 2017 by sama55
edited Jul 28, 2017 by sama55
  1. Create your layer plugin
  2. Add function below

public function nav_link($navlink, $class) {
    qa_html_theme_base::nav_link($navlink, $class);
    if($class == 'browse-cat') {
        $questions = qa_db_single_select(qa_db_qs_selectspec(qa_get_logged_in_userid(), 'created', 0, $navlink['label'], null, false, false, 3));
        if(count($questions)) {
            $this->output('<ul class="qa-'.$class.'-qs">');
            foreach($questions as $id => $question) {
                $this->output('<li>');
                $this->output('<a href="'.qa_q_path_html($id, $question['title']).'">');
                $this->output(qa_html($question['title']));
                $this->output('</a>');
                $this->output('</li>');
            }
            $this->output('</ul>');
        }
    }
}

Note:

Page display performance is not guaranteed.

commented Jul 31, 2017 by butterfly
@sama55

I did exactly what you suggested but didnt work. Actually you forgot to add "class qa_html_theme_layer extends qa_html_theme_base { " before the function :)

In the admin panel shows the plugin my-layer but in the front end when i click on the categories page it displays the categories as it was before the plugin (without listing the questions inside each category)

Did i miss something ??
Thanks
commented Jul 31, 2017 by sama55
It works properly in my test environment.

Learn how to make layer plugins on the following page.
http://docs.question2answer.org/plugins/layers/

And also, you can confirm that above function is running by echo.
For example ...
public function nav_link($navlink, $class) {
    qa_html_theme_base::nav_link($navlink, $class);
    echo 'This is function of my layer.'; // <= !!!
    if($class == 'browse-cat') {
...
}
If "This is function of my layer." is not shown, nav_link() function is not called by something cause. In this case, it is possible that your theme or any plugin has stopped layer function chain.
commented Aug 1, 2017 by butterfly
edited Aug 1, 2017 by butterfly
Thanks for your answer @sama55 i will check it asap and will let you know what i was doing wrong :)

UPDATE: i passed the echo in the function and it is displayed on each category, but the questions aren't. i double checked the code everywhere and is exactly as you mentioned above. !!!!!!!!!!!!!!!!!!!!
commented Aug 1, 2017 by sama55
$questions may be 0 (Zero).

public function nav_link($navlink, $class) {
    qa_html_theme_base::nav_link($navlink, $class);
    if($class == 'browse-cat') {
        echo 'label = '.$navlink['label']."<br/>"; // Category name
        $questions = qa_db_single_select(qa_db_qs_selectspec(qa_get_logged_in_userid(), 'created', 0, $navlink['label'], null, false, false, 3));
        echo 'questions = '.count($questions); // Result set
        ...
}
...