SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist WebMachine's Avatar
    Join Date
    Jun 2007
    Location
    Ontario, Canada
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    dynamically generated two-level menu - pages and categories

    I am working on my first Wordpress CMS (although I have a bit of experience in building basic Wordpress themes from scratch). This site has its 'posts' for all but one category on one of the pages, not the home page. I have a vertical menu in the sidebar that shows all the pages properly, but I am at a loss as to how to make the categories show up in a sub-menu for the page that contains the posts, unless I hard code the first level of the menu. I don't want to do that, because I want the client to be able to add pages if he wants.

    Ultimately the submenu will show up accordian-style (I also have the category links listed on the page itself for non-javascript people.) I have searched everywhere for a solution, but can't find one that mixes pages (level one) and categories (level two) in the menu. What code should I add here to make this work?

    PHP Code:
    <div id="sidebar">
        <div id="menu" role="navigation">
            <?php wp_nav_menu( array( 'container_class' => 'menu-header''theme_location' => 'primary' ) ); ?>
        </div><!-- #end of menu div -->

        <ul>
            <?php wp_register(); ?>
            <li><?php wp_loginout(); ?></li>
            <?php wp_meta(); ?>
        </ul>
        
    </div><!-- end of sidebar div -->

  2. #2
    SitePoint Evangelist WebMachine's Avatar
    Join Date
    Jun 2007
    Location
    Ontario, Canada
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please, someone. Two days and no response ... either what I am trying to do is impossible (please tell me and I will head a different direction) or the topic isn't interesting enough ... but I would really like to know how to do this. I am trying different tactics, but I know too little of the inner workings of Wordpress, so it's like wandering around blind-folded hoping I get lucky.

  3. #3
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are building your menu on the Appearance>Menu page in the backend of wp, you can add categories to the menu the same way pages are added. If you can't see the categories, use the screen options to show the category box.

    Once a category is added to the menu, drag and drop it under the page you want as the parent (drop it slightly to the right so it appears indented).

    If you do not want the top level text to be a link, you can do a custom link Menu Tip.

    Here's a link on how to use custom menus if you are unfamilar.

  4. #4
    SitePoint Evangelist WebMachine's Avatar
    Join Date
    Jun 2007
    Location
    Ontario, Canada
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your suggestion, sticks464. I was trying to avoid that, and use code because this is for a client and I don't want him to have to manually fix the menu every time he adds a static page or a new category to the site. The only way I can figure is to split the menu ... code in the 'home' and 'about' page links, and the 'projects' page will have the categories pulled in dynamically in the second level of the menu, then allow for the rest of the pages to be added dynamically after that. Or is there some way I can use php to 'find' the projects page link and add the submenu of categories to that, without disturbing the way the pages menu is generated?

  5. #5
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Web I am having trouble in what you are hoping to do. Can you give an example? Maybe your hierarchy?
    I can give you some code but I am not 100% sure what direction you are trying to go.
    Ryan B | My Blog | Twitter

  6. #6
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    You can get all pages by http://codex.wordpress.org/Function_Reference/get_pages
    try this is untested
    PHP Code:
    <a href="/">home</a>
    <?php
      
    IF(is_page('Projects')){
        
    get_pages('child_of=5555&sort_order=ASC&hierarchical=1');
      }else{
      
    get_pages();
      }
    ?>
    change 555 to the proper ID value
    Ryan B | My Blog | Twitter

  7. #7
    SitePoint Evangelist WebMachine's Avatar
    Join Date
    Jun 2007
    Location
    Ontario, Canada
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @rguy, I will try that out. By the way, I am having no problems getting the content where I want it, just structuring the menu. I want my menu to be like this:
    Home (page)
    About (page)
    Events (page with category-3 posts)
    Projects (posts - will not show any content at this level)
    - project 1 (category with posts)
    - project 2 (category with posts)
    - project 3 (category with posts)
    - ....
    Request for Info (contact form)
    Contact (page)

    I want the client to be able to add pages (level 1), and add categories (level 2) to the projects page, and have the menu be dynamically generated. My ultimate goal is to have an accordian effect to make the level 2 appear when the 'projects' is hovered over.
    Thank you.

  8. #8
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    It looks like you are intermixing a page and a category. Have a look at http://codex.wordpress.org/Pages
    You can create a page template that loads a certain category. I have a file in my theme folder called archives.php, that file's code looks like
    PHP Code:
    <?php
    /*
    Template Name: Archive Page
    */
    ?>
    <?php get_header
    (); ?>

    <div id="container">
        <div id="content-arch">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
            <div>
                <h2 class="post-title-single"><?php the_title(); ?></h2>
                <div class="post-entry">
                    <?php the_content(); ?>
                        <div style="clear:both;padding:0 1em 0 0;float:left;">
                            <h3 style="margin-bottom:0;">Categories</h3>
                            <ul style="margin-top:0;list-style-type:square;">
                                <?php wp_list_cats('sort_column=name&optioncount=1&feed=RSS&hierarchical=1'); ?>
                            </ul>
                        </div>
                        <div style="width:25%;padding:0;margin-left:1em;float:left;">
                            <h3 style="margin-bottom:0;">Monthly Archives</h3>
                            <ul style="margin-top:0;list-style-type:square;height:15em;overflow:auto;">
                                <?php wp_get_archives('type=monthly&show_post_count=1'); ?>
                            </ul>
                            <h3 style="margin-bottom:0;">Author Archives</h3>
                            <ul style="margin-top:0;list-style-type:square;">
                                <?php wp_list_authors('optioncount=1&exclude_admin=0&show_fullname=0&html=1');?>
                            </ul>
                        </div>
                        <div style="float:right;width:25%;">
    <script type="text/javascript"><!--
    //--></script>
                        </div>
                        <div style="clear:both;"></div>
    <h3>Tags</h3>
    <div style="padding:0.25em;"><?php wp_tag_cloud('order=ASC&orderby=name&number=0'); ?></div>
                        <div style="clear:both;"></div>
                    <?php edit_post_link('Edit this entry.''<p>''</p>'); ?>
                </div>
            </div>
    <?php endwhile; endif; ?>
        </div>
    </div>
    <?php get_footer(); ?>
    I have page called archive, which only has a title, and the page template is Archive page. The content of the page on the edit page screen is blank, because it is generated by archives.php. Pages themselves cannot have php (unless you have a plug in). I would make a project page template that grabs the project categories. If new categories will only made for project, not random thoughs (for example), you could grab all categories and excude non-project categories.
    Ryan B | My Blog | Twitter

  9. #9
    SitePoint Evangelist WebMachine's Avatar
    Join Date
    Jun 2007
    Location
    Ontario, Canada
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have the content appearing on my pages the way they should without a problem, including the page with the category, and the page with all the other categories. My issue is with the menu showing pages on the first level, and categories on the second level of the "projects" link ... and still having the pages and categories names show up in the menu dynamically. I think what I will do is hard-code the 'homepage', 'about' and 'projects' links in the menu so that I can have the second level show up dynamically, and then list the rest of the pages dynamically. That way the user can add or delete pages after the 'projects' page, and add or delete categories as he wishes. Will that work? (Remember it is just the vertical menu that I am looking at here).

    Home (page)
    About (page)
    Events (page with category-3 posts)
    Projects (posts - will not show any content at this level)
    ... menu items coded by hand
    - project 1 (category with posts)
    - project 2 (category with posts)
    - project 3 (category with posts)
    - ....
    Request for Info (contact form)
    Contact (page)
    ... called dynamically with Wordpress function

  10. #10
    SitePoint Evangelist WebMachine's Avatar
    Join Date
    Jun 2007
    Location
    Ontario, Canada
    Posts
    408
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I spoke with my client, and he's okay with just links to the categories on the page in question, so no need for this two-level menu ... I guess the problem is solved, but I would still love to know how to do this for the next time the need for it shows up. Thanks for your suggestions, anyhow.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •