Find Last Instance of a div class with php
I have generated a mega menu by extending the wordpress walker nav class.
To generate columns in the menu I have a div class nav_col that is floated.
I need to add a featured products area into the mega menu with a product link back to the shop pages.
In order to do this, I seem to have to be able to find the last instance of a div class in a menu, then add another instance of the same div nav_col directly after it to insert another column as a placeholder for the featured product.
Any Ideas??? Ive been stopped on this for a day or so now so any help would be awesome,
I would have thought there’s some standard tag matching algorithm floating around. I seem to recall seeing something even in WP, though I can’t find it now. Left to my own devices, I would probably brute force it. I assume you can find the first div in the menu. Search the remaining html for every
div>, keeping a running tally of the nest level by whether a slash precedes the find or not. Whenever a
</div>is found when the nest level is 0 is the matching closing tag. Then insert your nav_col content at position + 4.
I can see how that would work well if I always know the how many columns are in the menu. Or am I understanding correctly?
Perhaps give me example of the code structure you are talking about?
Ive been looking into a jquery solution…
Uh… I may be the one misunderstanding. I was assuming the menu had an overall div wrapper which you needed to find the closing tag for. In jquery you could just do
$(div.menu_wrapper).append('<div class="nav_col">html stuff</div>');No need for my clunky php solution.
But I think I see now, there’s a series of div blocks of unknown quantity for which you need to find the end. No wonder you’re stuck. You would need to somehow differentiate between menu columns and what ever comes after it. If the divs internal to the columns have a predictable class or something, you may have a chance, but if they could contain anything, there’s nothing to go on.
What about the column divs? Do they all have a particular class? There must be someway of identifying them. Can you do something like
$(div.menu_col:last).append('blah blah html');? Or identify them with some particular unique content? Without some representative html samples, I don’t think I can be of much more help. Hopefully I’ve sparked some idea which you can go on.
Yep I think were on the same page now. Theres a series of div blocks of unknown quantity for which I need to find the end, then insert another div, ie another column after that end. The structure is as follows:
<ul id=”nav” class=”menu”>
This was done by extending the wordpress walker. Grandparent classes are the root top level menu items, dropdown 1,2,3 ect form the mega menu fly out, the class “nav_col” forms the columns of the mega menu within each dropdown. So what I’d like to do is find the last instance of the div class “nav_col” within each dropdown class and insert another instance of the same class “nav_col” below it in the code such that an empty column is formed. Within that new instance of “nav_col” I would then make a function call to the function that would insert the featured product.
Jigoshop actually comes with a widget made for the sidebar that I may just be able to edit.
Using JQuery, something like
If using php, if we can be assured nav_col divs contain no internal divs, search for each div of class nav_col, then find the matching closing tag. The first time a div not of nav_col class is found, or end of file, go back to the last found closing tag and insert content afterwards.
If internal divs are possible, a variation of my nested level tally idea may work, eventually finding each closing tag of each nav_col still. I’m also assuming no divs not of nav_col class will occur between nav_col divs. A number of assumptions may indicate a flawed approach though.
Adapting existing code to your needs is the best solution, why reinvent the wheel?
- The topic ‘Find Last Instance of a div class with php’ is closed to new replies.