Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+9 votes
5.7k views
in Q2A Core by
I want to perform below same function in V1.2.
http://www.question2answer.org/qa/363/how-can-i-add-tag-to-sidebar
qa-page-home.php of V1.2 is many changed. If possible, teach us modification for V1.2.
Thanks in advance.

2 Answers

+5 votes
by
It's a similar idea, but the code is structured a little differently, so here goes...

Start by modifying function qa_home_load_ifcategory(...) in qa-page-home.php:

1. Add $populartags to the global list at the start of the function.

2. Change the code below:

        @list($questions1, $questions2, $questions3, $count, $categories, $categoryid, $custompage)=qa_db_select_with_pending($qa_db,
            $questionselectspec1,
            $questionselectspec2,
            $questionselectspec3,
            (isset($cachecountoption) && !isset($categoryslug)) ? qa_db_options_cache_selectspec($cachecountoption) : null,
            qa_db_categories_selectspec(),
            isset($categoryslug) ? qa_db_slug_to_category_id_selectspec($categoryslug) : null,
            $pageselectspec
        );

... to ...

        @list($questions1, $questions2, $questions3, $count, $categories, $categoryid, $custompage, $populartags)=qa_db_select_with_pending($qa_db,
            $questionselectspec1,
            $questionselectspec2,
            $questionselectspec3,
            (isset($cachecountoption) && !isset($categoryslug)) ? qa_db_options_cache_selectspec($cachecountoption) : null,
            qa_db_categories_selectspec(),
            isset($categoryslug) ? qa_db_slug_to_category_id_selectspec($categoryslug) : null,
            $pageselectspec,
            qa_db_popular_tags_selectspec(0)
        );

[All that changed is that $populartags was added to the end of the list(...) and qa_db_popular_tags_selectspec(0) was added at the end of the parameters to the qa_db_select_with_pending(...) call.]

3. Lower down in the same file, after qa_content_prepare(true, $categoryid); add the $populartags variable to $qa_content as follows:

$qa_content['populartags']=@$populartags;

4. Create a custom theme (see Advanced page on main site) and override the sidebar() function. Make sure your theme's sidebar() function calls the default one first via qa_html_theme_base::sidebar() otherwise your custom sidebar box will disappear. Then your function can do something with $this->content['populartags'], after checking isset($this->content['populartags']) since not every page will have it defined. To start with you can print_r($this->content['populartags']) to get an idea of what it contains. You can use qa_tag_html() to convert a tag to the standard HTML representation.
by
Thank you so much gidgreen!
Do not forget to include $populartags in @list.
by
Thanks Gideon, I made it until half of step 4) I print_r it and have an array in the sidebar, like this:

Array ( [faq] => 1 [help] => 1 [fifa] => 1 [soccer] => 1 )

Than i tried to convert it using qa_tag_html() but it did not result, as I really don´t know how to do so. You can see it on questions.com.mx (still unfinished)

Would be  very nice if You could aswell add this final step for the advanced theme.

Best regards, Monk333
by
Function in sidebar looks actually like that:

    function sidebar()
        {
            $sidebar=@$this->content['sidebar'];
           
            if (!empty($sidebar)) {
                $this->output('<DIV CLASS="qa-sidebar">');
                $this->output_raw($sidebar);
                $this->output('</DIV>', '');
            }

if (isset($this->content['populartags'])) {
$this->content['populartags'];
print_r($this->content['populartags']);
            }
        }
by
Ok, I think I could put it together, function sidebar() as follows,
it "seems to work", but I would happy if anybody could tell me if it is right and if someone could tell me how to substitute the domainname with the correct variable. Excuse me please for bad style but I hacked it together from a hanfull of tutorials and trial and error:

function sidebar()
        {
            $sidebar=@$this->content['sidebar'];
           
            if (!empty($sidebar)) {
                $this->output('<DIV CLASS="qa-sidebar">');
                $this->output_raw($sidebar);
                $this->output('</DIV>', '');
            }

                if (isset($this->content['populartags'])) {

                    $this->content['populartags'];

                    echo "<h2>Popular Topics </h2><br>";
                    foreach($this->content['populartags'] AS $name => $value)
                    {
                    echo "<a href=\"http://www.questions.com.mx/tag/$name/\">";
                    echo qa_tag_html($name);
                    echo "</a>";
                    echo $value." x<br>";
                       }
            }
        }
by
edited by
To avoid unlimited tags I added the function slice, so having just a fixed number of tags, in this case the top 12. As well some classes are new. They are added to the css as well to customize the design a bit.

Here we go, potential users would have to add the classes to the css and change the domainname questions.com.mx to something else.
I would be happy if someone could have a look to the code as I have no exact idea what I am doing there. But it works.

function sidebar()
{
    $sidebar=@$this->content['sidebar'];
   
    if (!empty($sidebar)) {
        $this->output('<DIV CLASS="qa-sidebar">');
        $this->output_raw($sidebar);
        $this->output('</DIV>', '');
    }
        if (isset($this->content['populartags'])) {


    $newarray=( array_slice( $this->content['populartags'], 0, 12 ) );



        $this->output('<DIV CLASS="qa-sidebar-poptags">');
                $this->content['populartags'];
        $this->output('<h2 CLASS="qa-sidebar-poptags-h2">');
                echo "Popular Topics";
        $this->output('</h2>', '');
        $this->output('<ul CLASS="qa-sidebar-poptags-ul">');

            foreach($newarray AS $name => $value)
            {
            echo "<li class=\"qa-sidebar-poptags-li\">";
            echo "<a class=\"qa-sidebar-poptags-link\" \"qa-sidebar-poptags-selected\" href=\"http://www.questions.com.mx/tag/$name/\">";
            echo $name;
            echo "</a> ";
            echo $value."x";
            echo "</li>";            }
        $this->output('</ul>', '');
        $this->output('</DIV>', '');
        }

}

Regards, Monk333
by
Awesome, works perfectly!
Thanks for sharing! :)
by
If You figure out which is the variable to substitute the domainname please let me know. Besides i think at least the line

echo "<a class=\"qa-sidebar-poptags-link\" \"qa-sidebar-poptags-selected\" ......

is not correct. But I think that, as long as someone doesnt show the tags on the selected tag site, the class \"qa-sidebar-poptags-selected\" is not used. So please help, I am not a coder.
by
R and other interested reraders,
to avoid showing up the count after the tags (helpful if You use the standard width of sidebar) just get rid of the line :

echo $value."x";

should work. And to change the amount of links shown change the line

 $newarray=( array_slice( $this->content['populartags'], 0, 12 ) );

where 12 is the number of tags shown. You may use any other number there.

Rgds Monk333
by
for domain name, i just replaces below code
******href=\"http://www.questions.com.mx/tag/$name/\">";******
by
******href=\"/tag/$name/\">";******
Works fine for me..
by
Thats it, and I was searching searching and searching...

Here are the classes to add to the qa-styles.css I am using on www.questions.com.mx

I know it is not well done, but again, it works:


.qa-sidebar-poptags {}
.qa-sidebar-poptags-h2 {color:#0B0B61; font-weight:bold;margin-right:5px;}
.qa-sidebar-poptags-ul {width:268px;border:1px solid #ccc;font-size:16px; list-style:none; padding:0; margin:18px 12px 18px 0;}
.qa-sidebar-poptags-li {margin:0.5em; margin-left:12px;margin-right:12px;background:#f0f0f0;border:1px solid #ccc;border-style:outset;padding:5px;}
.qa-sidebar-poptags-link {color:#045fb4;font-weight:bold;}
.qa-sidebar-poptags-selected,.qa-sidebar-poptags-selected:hover {text-decoration:none; color:#000;}

You MUST at least change the width in ul class if You are using the standard sidebar (my one is bit larger.)

Thanks again !
by
Yes, I already took those classes from your css file and modified as per my theme. :)

I added below line to add a link to all tags:
-----
        $this->output('<div style="text-align:right;"><a href="/tags">all tags »</a></div>', '');
-----
May help for other users!
by
edited by
Great, this is an important feature, will add it as well,

Thank You !

For those who want it inside the list as on my site (thanks to R) :

Add this following echo-line before   $this->output('</ul>', '');   :

echo "<li class=\"qa-sidebar-poptags-li\"><a class=\"qa-sidebar-poptags-link-all\" href=\"/tags/\">All Topics</a></li>";

And add this class to Your css:

.qa-sidebar-poptags-link-all {color:#ff3366;font-weight:bold;}

Should work !

I am sure that using the $this->output method would be better, but I only could work it our with echo.

Thanks again, R !
by
edited by
IMPORTANT:

The code so far works, but there is an issue with utf8 and special characters ! Where it says:

href=\"/tag/$name/\">";

You have to take away one slash

href=\"/tag/$name\">";

Otherwise when using tags like q&a or a&b and may be ä ö ü it wouldn´t work !
But it still does not validate against w3c Validation Service, will ask a related question now !
RGDS

monk333
by
thanks for the answers, does it work for the 1.3beta1 as well?
by
I will try to implement it again with my site, but I will wait for the 1.3 Release. However, if You give it a try meanwhile, please let us know..
by
edited by
YES IT STILL WORKS:

INSTRUCTION:

MAKE A BACKUP COPY OF ANY FILES YOU ARE GOING TO CHANGE !

CHANGE IN qa-page.home.php in the qa-include folder lines 37 to 54 as follows:

//    Common function to load the appropriate set of questions

    function qa_home_load_ifcategory($pagesizeoption, $feedoption, $cachecountoption, $allsomekey, $allnonekey, $catsomekey, $catnonekey,
        $questionselectspec1=null, $questionselectspec2=null, $questionselectspec3=null, $questionselectspec4=null, $pageselectspec=null)
    {
        global $categoryslug, $questions, $count, $categories, $categoryid,
            $pagesize, $showcategoryonposts, $sometitle, $nonetitle, $qa_template, $qa_content, $suggest, $showfeed, $qa_request, $populartags;
       
        @list($questions1, $questions2, $questions3, $questions4, $count, $categories, $categoryid, $custompage, $populartags)=qa_db_select_with_pending(
            $questionselectspec1,
            $questionselectspec2,
            $questionselectspec3,
            $questionselectspec4,
            (isset($cachecountoption) && !isset($categoryslug)) ? qa_db_options_cache_selectspec($cachecountoption) : null,
            qa_db_categories_selectspec(),
            isset($categoryslug) ? qa_db_slug_to_category_id_selectspec($categoryslug) : null,
            $pageselectspec,
            qa_db_popular_tags_selectspec(0)
        );





CHANGE IN THE SAME FILE AFTER LINE 243 AS FOLLOWS:

//    Prepare content for theme
   
    $qa_content=qa_content_prepare(true, $categoryid);
// ADDED POPULARTAGS BEGIN   
    $qa_content['populartags']=@$populartags;
// ADDED POPULARTAGS END


CLOSE AND SAVE THE FILE


GO TO YOUR ADVANCED THEME AND OPEN qa-theme.php


SEARCH THE FUNCTION function sidepanel() AND ADD $this->poptags(); AS FOLLOWS:

function sidepanel()
        {
            $this->output('<DIV CLASS="qa-sidepanel">');
            $this->sidebar();
            $this->nav('cat');

                       //ADDED POPULARTAGS CALL BEGIN           
                        $this->poptags();
                       //ADDED POPULARTAGS CALL END

SEARCH THE FUNCTION function sidebar AND ADD AFTER THAT FUNCTION AS FOLLOWS:

function sidebar()
        {
        .....
                .....
                .....
        }

// ADDED FUNCTION FOR POPULAR TAGS BEGIN

function poptags()
        {
      
            if (isset($this->content['populartags'])) {
      $newarray=( array_slice( $this->content['populartags'], 0, 10 ) );
        $this->output('<DIV CLASS="qa-sidebar-box">');
        $this->content['populartags'];
        $this->output('<h2 CLASS="qa-sidebar-h2">');
                echo "Top 10 Tags";
        $this->output('</h2>', '');
        $this->output('<ul CLASS="qa-sidebar-poptags-ul">');

      foreach($newarray AS $name => $value)
            {
            echo "<li class=\"qa-sidebar-poptags-li\">";
            $this->output(''.qa_tag_html($name).'');
            echo $value." x";
            echo "</li>";
            }
   
       
    echo "<li class=\"qa-sidebar-poptags-li\"><a class=\"qa-sidebar-poptags-link-all\" href=\"/tags/\">All Tags</a></li>";
        $this->output('</ul>', '');
        $this->output('</DIV>', '');
          }
      }

// ADDED FUNCTION FOR POPULAR TAGS END


SAVE AND CLOSE THIS FILE

NOW OPEN YOUR qa-styles.css FILE AND ADD THE FOLLOWING CLASSES:

.qa-sidebar-box{}
.qa-sidebar-h2{}
.qa-sidebar-poptags-ul{}
.qa-sidebar-poptags-li{}
.qa-sidebar-poptags-link-all{}

THERE YOU CAN SET SIZE COLOR AND SIMILAR AS YOU WANT.

Above in one comment are some examples for classes as well.

If You run in any trouble please post again.

You can change the order of the boxes in the sidebar by moving up or down the CALL for

$this->poptags();

You can change the texts
All Tags
Top 10 Tags
in the function how ever You want.

You can change the classnames as You like

You can stay without headline by deleting:

        $this->output('<h2 CLASS="qa-sidebar-h2">');
                echo "Top 10 Tags";
        $this->output('</h2>', '');

You can change the quantity of tags shown by changing the number 10 at the end of:

      $newarray=( array_slice( $this->content['populartags'], 0, 10 ) );

You can avoid showing the count of each tag by deleting:

    echo $value." x";

Good Luck
by
thank you very much monk!! I try it later! Big Thx!
by
Hi Monk,
what do you mean with:
function sidebar()
        {
        .....
                .....
                .....
        }

My qa-theme is basicly empty so I assume I have to add the functions you wrote above?

Thanks.
by
Out of my view it is easier to add all the funtions from qa-theme-base.php
beginning with:


function doctype()

more or less at the top of the file
until
the last one

function c_item_clear()
        {
            $this->output(
                '<DIV CLASS="qa-c-item-clear">',
                '</DIV>'
            );
        }

This is easier if You want to make more chnages.

Once ready, You can add after the

function sidebar()
{
.... means the whole sidebar function
}
by
The problem with adding all the functions is your theme is much more likely to have problems with a future version of Q2A. My recommendation is to add only those functions to your qa-theme.php file which you need to override.
by
edited by
Thanxs for summing up to monk333!
just a little typo: the href for "All Tags" is missing a dot "."
it must be:
echo "<li class=\"qa-sidebar-poptags-li\"><a class=\"qa-sidebar-poptags-link-all\" href=\"./tags/\">All Tags</a></li>";

****href=\"./tags/\****   instead of
****href=\"/tags/\****

and it only works for me without the "$count" in qa-page.home.php:
that means:
    global $categoryslug, $questions, $categories, ****
and delete the line:

            (isset($cachecountoption) && !isset($categoryslug)) ? qa_db_options_cache_selectspec($cachecountoption) : null,

after "    $questionselectspec4,"
by
it seems that version 1.5.x has the tag widget which can solve this question :D
0 votes
by
Using the Code on this page puts the tags on the questions page, but how do we get it on all pages?
...