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 ??

+5 votes
123 views
asked Jul 23 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 by butterfly
moved Jul 28 by Scott
Any help as it really drives me crazy ??
commented Jul 25 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 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 by sama55
edited Jul 28 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 Aug 1 by butterfly
edited Aug 1 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 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
        ...
}
...