• I have a menu structure that I want to use, but it would involve setting up a large number of custom menu links to create categories between a general title for a book, and the eventual product links.

    It’s like this:
    Book Title > Level 1 (2, 3, 4) > Part 1 (2, 3, 4) > Actual Book

    There are sixteen different books for each title. So all the “levels” and “parts” would be custom links for the menus, and are not real categories.

    I’m wondering if there ia a way to create a file that can be uploaded to the database to set this up in one shot rather than individually create the custom links in the dashboard. I’d be glad to type up a list and import it to the database.

    I have to do this for many titles, and it would be dozens and dozens of custom links I wouldn’t have to enter one at a time in the dashboard.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hi David

    Perhaps someone else will offer a better solution, but if it were me I would be tempted to just create a loop and put that in a widget where I want the menu displayed instead of using the WP custom menu function.

    Using a loop means that, depending on how your books are categorized and assuming that stays the same, your books could come and go and your menu would still work.

    In order to create a loop, we’d need to know more about how your books and the associated levels and parts are stored in your database – using a custom post type and metadata I assume?

    That said, perhaps there are easier ways to do this and I only say this because it sounds like you may be missing out on what is essentially the greatest power of WP, which is to organize content using taxonomies, or via page hierarchies.

    Alternatively, if you’re happy with the way the content is stored and you just want to craft a list of links *that will not change much, if at all, over time*, then you could also just create the HTML to build your menu structure and either put that in a widget OR in your header.php where the Theme would naturally call your menu.

    Thread Starter David Borrink

    (@davidborrink)

    Hmm, well that could be a neat way to do it, except that we have our Woocommerce store set up with the titles having a few different categories each. Your idea of having the taxonomies create the menus would probably create much bigger of a menu than we want to do. We came up with a structure that will get people to see what they want using a linear approach instead of three different paths by way of the different categories we set up.

    So a manual entering would suit us better. That’s why I’d like to type it up and import it, or set it up as a database upload if I could find out what I’d need to enter.

    If you don’t mind more questions, how are the levels and parts structured? Hierarchical custom taxonomies?

    E.G.
    Book Title 1
    – Level 1
    – Part 1
    – Part 2
    – Part 3
    – Part 4
    – Level 2
    – Part 1 (assuming this would have a different slug than the first part 1)
    – Part 2
    – Part 3
    – Part 4
    etc.
    Book Title 2
    – Level 1
    – Part 1
    And so on…..?

    If this is the case, then a loop that first gets all the taxonomies, and then queries for all of your CPT “books” for those that have those terms assigned may work.

    That said, I’m not sure that get_terms includes ‘depth’ as a parameter, meaning it may not handle this many levels of terms – I use it for single-level taxonomy queries, BUT if it won’t do depth, you *could* just repeat the “get_terms” part using “child_of” and hard-coding the taxonomy ID for each one….then you’d only need to modify your loop IF you add or change parent taxonomies.

    SO I just used this example for another person in the forum who is doing something different, but with a similar solution – in this code I use get_terms to get all the terms within a specific custom taxonomy (in my case, it’s ‘retail_category’, if your terms are as above, then in your case it might be ‘book_title’). Once I have all the terms, the for each term, I do a query to find all the posts within a CPT that have that term assigned, and I echo the post title(s).

    $terms = get_terms( 'retail_category', array('orderby' => 'name', 'order' => 'ASC') );
       if ($terms) {
         foreach($terms as $term) {
         $args = array(
          'post_type' => 'retail',
          'posts_per_page' => -1,
          'ignore_sticky_posts' => 1,
          'orderby' => 'title',
          'order' => 'ASC',
          'tax_query' => array(
                array(
                'taxonomy' => 'retail_category',
                'field' => 'name',
                'terms' => $term->name)
          )
        );
        global $post;
        $myposts = new WP_Query($args);
          if ( $myposts->have_posts() ) {
            echo '<ul><li><h5 class="retailcatname">' . $term->name . '</h5><ul>';
            while( $myposts->have_posts() ) : ($myposts->the_post());
               echo '<li><a href="<?php the_permalink() ?>">';
               echo the_title();
               echo '</a></li>';
             endwhile;
            echo '</ul></li></ul>';
       }
     }
    }

    Instead of just echoing the titles, you’d also want to wrap it in some div tags using the WP navigation selectors so you can style it to look menu-like.

    If you modify this and it works, then you have a few options for how to get it on your page – you could create a template file that you call in your header, like so:

    include(STYLESHEETPATH . '/_menutop.php');

    Using an underscore will keep this file at the top of your template files in your Theme Editor, so you can quick find and modify it as needed.

    OR you could put it in a widget and place the widget in a widget area you’ve in your header.php…..OR you could put it in your functions.php (or better yet, a my-functions.php that you put in your plugins folder and activate) then either use a shortcode or just the PHP function where you need it, with or without using a widget.

    I hope this helps….

    Thread Starter David Borrink

    (@davidborrink)

    Trisha,

    First, I’ll explain my hierarchy for my client’s products. I just re-examined how I was going to set this up, and I’m beginning to think you might have my solution. Here’s how the store is set up.

    All the books are categorized in two ways:
    1) by topic
    2) by level.

    So I have two parent product categories of
    1) Studies – By Topic
    2) Studies – By Level

    Then I have all the child categories of topic names which are under the first one, and all the age level under the other.

    So each book can be categorized with a title and a level. It’s really that simple. Now the quantity of categories and titles vary. There are currently 14 topics, and most of those have 1 to 3 titles each, but two of them have 20 and 21 titles. The Level listings have 7 age groups, and 8-10 titles on each.

    (There actually are other categories like “Dent and Ding” for slightly damaged books at a discount. I might add or omit that pending my client’s directions.)

    See, I was thinking that I’d keep the menus simpler looking by having the levels and parts be in two separate menus so they’d be 5 items tall at the most, but that is another level of structure that can only be done manually because it’s not really my product category structure. I don’t have a Level 1, 2, 3, 4 sub-category and then a Part 1, 2, 3, 4. It’s really the product as Level 1 Part 1, Level 1 Part 2, etc., so I’m think my manual method doesn’t reflect the true structure.

    Your method calls up the terms in the taxonomy and looks for the titles, or product names in my case. And the thing about your ideas is that my client can add a new title and it would automatically show up in this menu! No additional hand-coding would be needed to add a title.

    My plan is to put this on a mid-front page area in an area of widgets, and put it in a navigation menu similar to the main navigation menu. So I’d pick up on the styles I have but modify them to look different than the main navigation. So I’d put this in it’s own template file, wrap it in some divs with the right styling in my CSS and call it into my custom front page template.

    Any thought from you before I would try this, based on my new info?

    Thanks for your time with me on this. πŸ™‚

    Thread Starter David Borrink

    (@davidborrink)

    And to clarify, I would not put this in a widget, but in a block next to other widgets that position next to each other.

    1. I assume that ‘retail_category’ would need to be replaced by one or two of my parent categories? I have two, as mentioned above, or is there a single name that covers those?

    2. My post type would be “products”, so that post_type line would need to reflect that.

    3. The <h5> isn’t used by my menus, so I’m assuming I can drop that and use the styling I have in my navigation menus if I wrap a div around this whole thing.

    4. I’ve never done a call-in of another template into a template so I’m not sure of the syntax to do this. I understand I put the include(STYLESHEETPATH . '/_filename.php'); in the header.php file, but how do I call this up in my front page template?

    <div id="main-navigation" class="product-menu">
        WHAT DO I PUT HERE?
    </div>
    Thread Starter David Borrink

    (@davidborrink)

    Oh wait. I just re-read your post above and I think you answered most of my questions. I can just put this code in my front page template as it’s only for the front page.

    The only question would be the “retail_category” one. That is my question #1 above. That’s the only one I don’t understand. In your example above is “retail_category” a parent category? I just checked my categories again and I have five top level categories. Two major and three minor. So do I need to run five loops, one for each parent category? Or can they be combined? Should I set up a higher level category to encompass the whole store?

    I’m glad to be learning how to do this. I just want to “get it”. πŸ™‚

    Isn’t it fun when you’re learning new stuff? πŸ™‚

    It sounds like you have a good understanding – but I’d like to repeat back to you what I’m getting to be sure we’re both on the same page.

    Your CPT is ‘products’ (although it’s considered good practice to name your CPTs by their singular, not plural, instance), and you have two custom taxonomies, ‘studies by topic’ and ‘studies by level’. Within each of those two custom taxonomies you have parent terms and child terms.

    I still think this is very do-able, but it sounds like you will have to nest in another level to get the ‘child’ terms for each (parent) term – WP has a function for this, it’s “get_term_children”…you can read about it here:

    https://codex.wordpress.org/Function_Reference/get_term_children

    I haven’t monkeyed about with that much yet because my taxonomies are flat, but the examples on that page are pretty good.

    OR it might be simpler, given that you have only two custom taxonomies, to consider two menu structures, allowing site visitors to browse the title by *either* Topic or Level. Then you could do a simple single taxonomy loop, but repeat it, and hard code in the taxonomy each time.

    If you get stuck after looking at the examples at that link, post back and I’ll have a crack at some code – no guarantees but we should be able to get you there.

    Thread Starter David Borrink

    (@davidborrink)

    Great. I’ll look into that “get_term_children” reference and check out the examples.

    Actually my CPT is “product” so I’m singular there. πŸ™‚

    Thread Starter David Borrink

    (@davidborrink)

    Well that was a shorter read than I expected! I kind-of get it. Okay so it looks like it’s time to code this.

    I’m not clear about how to put more than one parent term in the example code above. Do I replace “retail_category” in your example with “product”? How do I tell it to look for just the parent categories of “studies-by-topic” and “studies-by-level”? (I’m thinking of not including the three other smaller parent categories.) Do I put those in place of “retail-category”? I sort-of understand “array” and the need to put stuff in between the ( ), and I know enough of PHP to do some things, but not enough to prevent disaster LOL. If you’re willing to have a crack at some code, as you said, then I’m grateful. I want to do this but I need a little hand-holding when it comes to doing get/array stuff. I like help, but I want to “get it” so I can do it myself in the future. πŸ™‚

    So here’s what I want: my CPT is “product”. My two main parent categories are “studies-by-topic” and “studies-by-level”. Then the child category level is under that with the category for titles. So how would I nest a level of child searches? Those child categories contain the actual books.

    Ultimately there would be three levels of menus here:
    1) find a topic or level (two choices),
    2) topics (14 choices) / levels (7 choices)
    3) titles of each topics in this menu

    That’s where your “expertise” will have to inform me. Obviously you really enjoy this, so I’m grateful. πŸ™‚

    OK in order to answer your questions I need to be sure I’m understanding how you have your ‘categories’ setup – it *sounds* like you’re using the standard WP ‘categories’ feature, with two parent categories and child categories below that (initially I assumed that you created custom taxonomies, but now I’m pretty convinced that you’re just using Categories, so until you correct me I’m going with that assumption).

    (Sidenote, this next bit is just to help you, forgive me if you already have a good handle on this, it may help others in the future) In the last couple of years, since WP started supporting custom post types and custom taxonomies, it altered the concept of categories, tags, and custom taxonomies so that ALL (each) are now just considered a ‘taxonomy’….so “categories” is a taxonomy, “tags” is a taxonomy, and in my case “retail categories” is a taxonomy.

    Within each taxonomy you have ‘terms’. So in your case, using the taxonomy of ‘categories’ you have a term called ‘studies by topic’ (with child terms below that) and a term called ‘studies by level’ (with child terms below that one).

    Taxonomies can be hierarchical (with parent/child terms, like the standard categories) or flat, like the standard tags. When you create a custom taxonomy, you set a parameter for that to true or false. AND taxonomies can be available to all post types, or just custom post types, again depending on parameters that are set when you create a CPT or custom taxonomy.

    Now for your question, in my case ‘retail_category’ is a *taxonomy* that applies to my CPT of ‘retail’ (which if I were being grammatically correct should be ‘retailer’ but the site owner insisted on ‘retail’…sigh).

    YOUR CPT is ‘product’ (yay on the singular!). Your *taxonomy* (based on my assumption above) is ‘category’.

    Now onto some code – as previously mentioned it might be easiest for your site visitor to see two ‘menu’ options to browse, so this is a loop to show all posts within a sub-category of a parent category, and you’d repeat this loop using the (second) category where you want the second menu to show up.

    Change any of this as you see fit, for example you may not want a h3 tag for your term title, you may be nesting UL’s instead):

    $cat = 'studies-by-topic';
    $catID = get_cat_ID($cat);
    $subcats = get_categories('child_of=' . $catID);
        foreach($subcats as $subcat) {
        echo '<h3>Book about ' . $subcat->cat_name . '</h3>';
        echo '<ul>';
        $subcat_posts = get_posts('cat=' . $subcat->cat_ID);
        foreach($subcat_posts as $subcat_post) {
            $postID = $subcat_post->ID;
    	echo '<li>';
    	echo '<a href="' . get_permalink($postID) . '">';
    	echo get_the_title($postID);
    	echo '</a></li>';
    	}
    	echo '</ul>';
    	}

    Then just repeat that with your next parent category, studies by level (I used the slug above, you can use name if you like).

    You *can* do this with a nested loop and have one menu with sub-lists, but it requires getting the parent category first, then for each parent category query for all child-categories, then find the posts – it’s doable if you want to go that route, I’d just have to put more time into the code, and honestly I’m not a coder – I’ve just learned how to find and adapt others’ code for my use.

    I hope this helps – I’m very anxious to learn how it turns out – would love to see it if you get it working. πŸ™‚

    P.S. There may be some other creative ways to structure your menu – jump links for example…..you might consider checking to see if there is a WP Users Group in your area (meetup.com is a great place to look) – find an advanced user who is also a coder and make friends with him or her. πŸ™‚

    Thread Starter David Borrink

    (@davidborrink)

    Trisha, sorry for the delay in responding but thank you so much for all this. I will finally be implementing this shortly (after juggling so many other projects), and will report back how it works.

    I’m definitely going to try the “two menus” option that you suggested, as that makes better sense given how things can be set up.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Can a menu structure be imported?’ is closed to new replies.