Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+3 votes
3.8k views
in Themes by
How i can add additional custom menu items at top user bar.. here is the code can anyone suggest

 

$this->output('<DIV CLASS="qa-top-header">', '');
            $this->nav('user');
            $this->output('</DIV>', '');

2 Answers

+1 vote
by
selected by
 
Best answer

Based on the fact that you want to add items on the same menu then I'd say you should add it to the same menu :)

Go to your qa-theme.php file and add this:

function doctype() {
   qa_html_theme_base::doctype();
   $menu_items = array(
      'menu1' => array(
         'url' => 'http://www.google.com',
         'label' => 'Google!',
      ),
      'menu2' => array(
         'url' => 'http://www.yahoo.com',
         'label' => 'Yahoo! (Yes, it still exists!)',
      )
   );
   $this->content['navigation']['user'] = array_merge($this->content['navigation']['user'], $menu_items);
}

This will insert the new menu items after the 'login' link when the user is not logged in and after the logout link when the user is logged in.

This will create similar-looking fields as the theme is responsible for the CSS. However, if you still want to override some CSS from the theme you can do so with the CSS classes .qa-nav-user-menu1 and .qa-nav-user-menu2.

Screenshot for the Classic theme with this change on the HOT page.

by
it is hiding only when clicking on menu items like hot, tags etc
by
Have you tested this as I suggested? As I mentioned before, it is working for every page and not a selected group of pages. Test it on any other theme and you'll see it is working. I added a screenshot showing you the change. If it is not working on your particular theme, you should correct the theme. I can't do much more about this issue
by
<?php
$theme_dir = dirname( __FILE__ ) . '/';
$theme_url = qa_opt('site_url') . 'qa-theme/' . qa_get_site_theme() . '/';
qa_register_layer('/qa-admin-options.php', 'Theme Options', $theme_dir , $theme_url );

//var_dump($theme_dir);

    class qa_html_theme extends qa_html_theme_base
    {

        function head_metas()
        {
            qa_html_theme_base::head_metas();
            $this->output('<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">');
        }
       
        function head_script()
        {
            qa_html_theme_base::head_script();
           
            $this->output('
                <script type="text/javascript">
                $(document).ready(function(){
                    $(".menu_show_hide").click(function(){
                    $(".qa-nav-main").slideToggle();
                    });

                $(window).resize(function() {
                    if ($(window).width()>720) {$(".qa-nav-main").show();}
                });
                }
                );
                </script>');

        }
        function head_css()
        {
            if (qa_opt('qat_compression')==2) //Gzip
                $this->output('<LINK REL="stylesheet" TYPE="text/css" HREF="'.$this->rooturl.'qa-styles-gzip.php'.'"/>');
            elseif (qa_opt('qat_compression')==1) //CSS Compression
                $this->output('<LINK REL="stylesheet" TYPE="text/css" HREF="'.$this->rooturl.'qa-styles-commpressed.css'.'"/>');
            else // Normal CSS load
                $this->output('<LINK REL="stylesheet" TYPE="text/css" HREF="'.$this->rooturl.$this->css_name().'"/>');
           
            if (isset($this->content['css_src']))
                foreach ($this->content['css_src'] as $css_src)
                    $this->output('<LINK REL="stylesheet" TYPE="text/css" HREF="'.$css_src.'"/>');
                   
            if (!empty($this->content['notices']))
                $this->output(
                    '<STYLE><!--',
                    '.qa-body-js-on .qa-notice {display:none;}',
                    '//--></STYLE>'
                );
        }       
       

        function body_content()
        {
            $this->body_prefix();
            $this->notices();
           
            $this->output('<DIV CLASS="qa-top-header">', '');
                        
            $this->nav('user');
            $this->output('</DIV>', '');

            $this->header();
            $this->output('<DIV CLASS="qa-body-wrapper">', '');
               
            $this->widgets('full', 'top');
           
            $this->output('<DIV CLASS="qa-sub-nav">');
           
            $this->nav('sub');
           
            $this->output('</DIV>');
           
            $this->widgets('full', 'high');
                        $this->nav_user_search();
            $this->sidepanel();
                        
            $this->main();
                        $this->widgets('full', 'low');
                        
            $this->output('</DIV> <!-- END body-wrapper -->');
           
            $this->footer();
                        
            $this->widgets('full', 'bottom');

            $this->body_suffix();
        }
       
        function header()
        {
            $this->output('<DIV CLASS="qa-header">');
           
            $this->logo();$this->output('<div id="header-text"><div class="header-name"><br/>
    <form action="./?qa=ask&cat=" method="POST">
                    
                    <INPUT NAME="title" TYPE="text" CLASS="qa-form-tall-text custom-ask-text" value="" placeholder="type your question here..." autocomplete="off" role="textbox">
                    <INPUT CLASS="custom-ask-submit" TYPE="submit" value="Ask Question">

            <INPUT TYPE="hidden" NAME="doask1" VALUE="1">
        </form>

</div></div>');
            $this->nav_main_sub();
            $this->header_clear();
                        
           
            $this->output('</DIV> <!-- END qa-header -->', '');
        }
       
        function nav_user_search()
        {
            $this->search();
        }
       
        function nav_main_sub()
        {
           $this->nav('main');
        }

        function nav($navtype, $level=null)
        {
            $navigation=@$this->content['navigation'][$navtype];
            if ($navtype=='main'){
                $this->output('<nav id="mobilenav"><a href="#" class="menu_show_hide">Menu</a></nav>');
            }
           
           
            if (($navtype=='user') || isset($navigation)) {
                $this->output('<DIV CLASS="qa-nav-'.$navtype.'">');
               
                if ($navtype=='user')
                    $this->logged_in();
                   
                // reverse order of 'opposite' items since they float right
                foreach (array_reverse($navigation, true) as $key => $navlink)
                    if (@$navlink['opposite']) {
                        unset($navigation[$key]);
                        $navigation[$key]=$navlink;
                    }
               
                $this->set_context('nav_type', $navtype);
                $this->nav_list($navigation, 'nav-'.$navtype, $level);
                $this->nav_clear($navtype);
               
                $this->clear_context('nav_type');
   
                $this->output('</DIV>');
            }
        }

        function nav_item($key, $navlink, $class, $level=null)
        {
            $this->output('<LI CLASS="qa-'.$class.'-item'.(@$navlink['opposite'] ? '-opp' : '').
                (@$navlink['selected'] ? (' qa-'.$class.'-item-selected') : '').
                (@$navlink['state'] ? (' qa-'.$class.'-'.$navlink['state']) : '').' qa-'.$class.'-'.$key.'">');
            $this->nav_link($navlink, $class);
           
            if (count(@$navlink['subnav']))
                $this->nav_list($navlink['subnav'], $class, 1+$level);
           
            if ($class=='nav-cat'){
               
                    $neaturls=qa_opt('neat_urls');
                    $url=qa_opt('site_url');
                    $mainkey=$key;
                    if ($key=='all')$key='.rss';else $key='/'.$key.'.rss';
                   
                    switch ($neaturls) {
                        case QA_URL_FORMAT_INDEX:
                                $url.='index.php/feed/questions'.$key;
                            break;
                           
                        case QA_URL_FORMAT_NEAT:
                            $url.='feed/questions'.$key;
                            break;
                           
                        case QA_URL_FORMAT_PARAM:
                            $url.='?qa=feed/questions'.$key;
                            break;
                           
                        default:
                            $url.='index.php?qa=feed&qa_1=questions&qa_2='.$mainkey.'.rss';
                       
                        case QA_URL_FORMAT_PARAMS:
                            $url.='?qa=feed&qa_1=questions&qa_2='.$mainkey.'.rss';
                            break;
                    }
                    $this->output('<A HREF="'.$url.'" CLASS="qa-cat-feed-link"><DIV CLASS="qa-feed-cat"></DIV></A>');
                }
           
            $this->output('</LI>');
        }
                


function doctype() {
    qa_html_theme_base::doctype();
    $categories = isset($this->content['navigation']['cat']) ? $this->content['navigation']['cat'] : null;
    if (isset($categories)) {
        foreach ($categories as $name => $category) {
            unset($this->content['navigation']['cat'][$name]['note']);
        }
$menu_items = array(
      'menu1' => array(
         'url' => 'http://www.thegeneralstuff.com/listuploads',
         'label' => 'Uploads',
      ),
'menu2' => array(
         'url' => 'http://www.thegeneralstuff.com/sitemap.xml',
         'label' => 'Sitemap',
      ),
'menu3' => array(
         'url' => 'http://www.thegeneralstuff.com/feedback',
         'label' => 'Feedback',
      ),
      'menu4' => array(
         'url' => 'http://www.thegeneralstuff.com/we-are-hiring',
         'label' => 'We are Hiring',
      )
   );
   qa_array_insert($this->content['navigation']['user'], 'login', $menu_items);

 $anonymous = qa_lang_html('main/anonymous');
    if (isset($this->content['q_view']['who']['data']) && strpos($this->content['q_view']['who']['data'], 'class="qa-ip-link">' . $anonymous . '</a>') !== FALSE) {
        $this->content['q_view']['who']['data'] = $anonymous;
    }


            if(isset($this->content['a_form'])) {
                $a_form = $this->content['a_form'];
                unset($this->content['a_form']);
                $this->content['a_form'] = $a_form;
            }    
   }
}
  
function view_count($post)
        {
            // do nothing
        }
        function theme_view_count($post)
        {
            qa_html_theme_base::view_count($post);
        }
       
        function post_meta_flags($post, $class)
        {
            $this->theme_view_count($post);
            qa_html_theme_base::post_meta_flags($post, $class);
        }
       
        function footer()
        {
            $this->output('<DIV CLASS="qa-wrap-footer">');
           
            qa_html_theme_base::footer();
           
            $this->output('</DIV> <!-- END qa-footer -->', '');
        }
       
    }

/*
    Omit PHP closing tag to help avoid accidental output
*/

I didn't figure it out pl help
by
edited by
You've added the code I've given you inside the " if (isset($categories)) { ... }". Actually a lot of code is in there and that does not make sense. Should look like this (pay more attention to the curly braces):

function doctype() {
    qa_html_theme_base::doctype();
    $categories = isset($this->content['navigation']['cat']) ? $this->content['navigation']['cat'] : null;
    if (isset($categories)) {
        foreach ($categories as $name => $category) {
            unset($this->content['navigation']['cat'][$name]['note']);
        }
    }
    $menu_items = array(
        'menu1' => array(
            'url' => 'http://www.thegeneralstuff.com/listuploads',
            'label' => 'Uploads',
        ),
        'menu2' => array(
            'url' => 'http://www.thegeneralstuff.com/sitemap.xml',
            'label' => 'Sitemap',
        ),
        'menu3' => array(
            'url' => 'http://www.thegeneralstuff.com/feedback',
            'label' => 'Feedback',
        ),
        'menu4' => array(
            'url' => 'http://www.thegeneralstuff.com/we-are-hiring',
            'label' => 'We are Hiring',
        )
    );
    $this->content['navigation']['user'] = array_merge($this->content['navigation']['user'], $menu_items);

    $anonymous = qa_lang_html('main/anonymous');
    if (isset($this->content['q_view']['who']['data']) && strpos($this->content['q_view']['who']['data'], 'class="qa-ip-link">' . $anonymous . '</a>') !== FALSE) {
        $this->content['q_view']['who']['data'] = $anonymous;
    }

    // Code below makes no sense!
    if (isset($this->content['a_form'])) {
        $a_form = $this->content['a_form'];
        unset($this->content['a_form']);
        $this->content['a_form'] = $a_form;
    }
}

Also note my other answer on removing category numbers has another function to add to the qa-theme.php. You're only removing the first level category counts.
+1 vote
by
After first line add this

$this->output('<div class="new-top-bar"> add your menu code here </div>');
by
Yes thanks for reply.. submit your website in our showcase if you have Q2A powered website

http://www.thegeneralstuff.com/tgs-community/q2a-showcase
by
$this->output('<div class="new-top-bar"> add your menu code here </div>');
$this->nav('user');

creating two menu line how to combine in one line
by
What are you looking for exactly? Adding new menu items immediately on the left of the 'Login' link?
by
no i want to add additional links in same menu
...