Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
602 views
in Q2A Core by

Just found out that if you add the postid of an answer after stackoverflow.com/questions/ID then stackoverflow redirects to the stackoverflow.com/questions/questionID

This is a fantastic feature that we could implement into the core!

(I could also write a plugin for that, but it's so awesome to deserve the core :) )

PS: At Stackoverflow it only works for Q and A, we could also include Comments ;-)

Q2A version: 1.8.0
by
moved by
I have been looking forward to implement this without success.alternatively I want to do away with question id for good and have something like domain/here-is-question

1 Answer

+1 vote
by
The following is one possible implementation. As long as it is not in the core, copy it into your qa-theme.php:

Better output (there is still no button in the q2a forum CKEditor to insert code with indents, damn): https://jsfiddle.net/4zyf0o6j/

function initialize() {
  if($this->template == 'not-found')
    {
        // remove slash from beginning of string
        $uri = ltrim($_SERVER['REQUEST_URI'], '/');
        if(is_numeric($uri))
        {
            $postid = (int)$uri;
            $parentpost = qa_db_read_one_assoc(
                            qa_db_query_sub('
                                SELECT `parentid`, `type` FROM ^posts
                                WHERE `postid` = #', $postid),
                            true);
            
            if(!empty($parentpost['parentid']))
            {
                if($parentpost['type'] == 'A')
                {
                    // get question title
                    $qtitle = qa_db_read_one_value(
                                    qa_db_query_sub('
                                        SELECT `title` FROM ^posts
                                        WHERE `postid` = #', $parentpost['parentid']),
                                    true);
                    
                    $questionurl = qa_q_path($parentpost['parentid'], $qtitle, false, 'A', $postid);
                    
                    // redirect to question
                    qa_redirect_raw($questionurl);
                }
                else if($parentpost['type'] == 'C')
                {
                    // comment parent is Q or A
                    $grandparent_post = qa_db_read_one_assoc(
                                    qa_db_query_sub('
                                        SELECT `postid`, `parentid`, `type`, `title` FROM ^posts
                                        WHERE `postid` = #', $parentpost['parentid']),
                                    true);
                    
                    if($grandparent_post['type']=='Q')
                    {
                        $questionurl = qa_q_path($grandparent_post['postid'], $grandparent_post['title'], false, 'Q', $postid);
                        
                        // redirect to question
                        qa_redirect_raw($questionurl);
                    }
                    else if($grandparent_post['type']=='A')
                    {
                        // parent of answer is question, get question title
                        $qtitle = qa_db_read_one_value(
                                    qa_db_query_sub('
                                        SELECT `title` FROM ^posts
                                        WHERE `postid` = #', $grandparent_post['parentid']),
                                    true);
                        
                        $questionurl = qa_q_path($grandparent_post['parentid'], $qtitle, false, 'C', $postid);
                        
                        // redirect to question
                        qa_redirect_raw($questionurl);
                    }
                    
                }
            }
        }
    } // END if($this->template == 'not-found')
} // END initialize()
...