Conversation started Jan 4, 2016 at 19:06.
Jan 4, 2016 19:06
Quick question: Anyone know why the depth attribute of the item passed into start_el in a custom nav walker would always be zero even when it has a parent assigned?
@NathanArthur - Could it be that you need to use start_lvl instead of start_el? I've gotten depth to work with end_lvl but I can't say I've tested it with *_el functions.
@Howdy_McGee Could be. It's a simple walker that only uses start_el. No other methods. Any idea how I could prevent the elements from being outputted if I used start_lvl?
Nevermind, I was able to get depth to work on hierarchical items, 0 top, 1 child, 2 grandchild using start_el method
Yeah. Something must be fishy with my setup.
@NathanArthur so if you do $class_names .= " {$depth}"; it's 0 on every. single. list item?
Jan 4, 2016 19:21
I did this:
var_dump('depth comp');
var_dump($depth);
var_dump($args['depth']);
And I got this:
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
string(10) "depth comp" int(0) int(1)
I tried pasting your method but I couldn't get it to work properly, here's maybe a simpler method you can work with that adds $depth to the class - maybe you can reverse-engineer what the issue is pastebin.com/bwnLMc3F
Thanks! I should probably recode the whole thing. It's a modified version of a walker I found somewhere online, so it's pretty messy.
Do you normally have to worry about preventing items below the depth arg from getting displayed in your custom walker, or does WordPress do that automatically?
Depth arg, meaning the depth limit set when you call wp_nav_menu
I feel like something else is wrong. The object I'm getting doesn't have attributes you're referencing.
Doesn't have ->classes, ->attr_title, ->target, ->xfn, or ->url
@How
* @Howdy_McGee I'm getting a WP_Post object. What type of object should I be getting?
Jan 4, 2016 20:14
@NathanArthur They definitely are objects but they are type of nav_menu_item objects. You can see here what the object contains, object_id being the $post_id and ID being the Item ID.
So You can't use $item->ID to get post meta, unless you're assigning post meta onto the Nav Menu Item ( which I doubt ). You would need to use $item->object_id.
This probably explains why your method returns early and doesn't display any actual list items
Jan 4, 2016 20:48
@Howdy_McGee Problem is my code does work, at least partially. I do get nav items out of it. I think the objects getting passed into start_el may be different from what I should be getting some how.
Here's what they look like:
object(WP_Post)#688 (24) {
  ["ID"]=>
  int(22)
  ["post_author"]=>
  string(1) "1"
  ["post_date"]=>
  string(19) "2015-10-18 14:52:31"
  ["post_date_gmt"]=>
  string(19) "2015-10-18 14:52:31"
  ["post_content"]=>
  string(34) "This is the college category page."
  ["post_title"]=>
  string(7) "Academy"
  ["post_excerpt"]=>
  string(0) ""
  ["post_status"]=>
  string(7) "publish"
  ["comment_status"]=>
  string(6) "closed"
  ["ping_status"]=>
  string(6) "closed"
  ["post_password"]=>
  string(0) ""
Weird, do you have a pre_get_post somewhere in your theme or plugins?
Those are definitely pages and not nav_menu_item's
I deactivated all other plugins. I'll do a search for pre_get_post
"No occurrences found"
I can't imagine why your wp_nav_menu() walker is spitting our pages instead of nav_menu_items
Super strange
Could a plugin change be retained even after it's deactivated?
Like something on a hook or filter?
Hopefully not, I really doubt it. I'm assuming you're running the latest stable version of WP - 4.4?
Jan 4, 2016 21:03
Yeah, just updated it today. Let me spin it up and check now though.
Maybe try switching to the default 2016 theme ( or 2015, whichever )
I guess then I'd have to copy my code into that theme?
Just the walker really. This will let you know if it's the walker or not
That will narrow it down to what else could be causing the issue
Running 4.4
I think I already know it's not my walker since your code pulls in the same object.
Is my logic flawed?
The only thing I added to your code was
class SecondaryNavWalker extends Walker_Nav_Menu {
The walker should be passing a Nav Item Object - by default in a normal install. I don't see anywhere in the walker itself where you're replacing the $item with a $post and the walker method I threw in pastebin definitely doesn't do any of that.
But, if you want to be certain if it's your walker or not you could throw it into a default theme
Jan 4, 2016 21:10
Ok, I'll try it.
btw, here's the whole functions.php file just incase I did something stupid: pastebin.com/pETtzX39
It's based on the blankslate starter theme
Still giving me WP_Post
I'm going to try sticking your code into twentyfifteen
Same with your code in twentyfifteen. Maybe core got modified some how? Is there an easy way to check that?
Jan 4, 2016 21:26
Maybe, I'm not sure of a good way to test that. If I paste your entire functions.php into my test one - everything works as expected. I get the depth as a class.
wp_nav_menu( array( 'menu' => 'Test', 'walker' => new SecondaryNavWalker() ) );
This is crazy. Maybe I just need to reinstall WordPress.
Haha idk but that sounds like a good idea - very strange issue
Seems like wp-cli can check core, but getting it to work with bitnami creates a whole new issue
Sounds like you're localhost, yeah? Probably easier to create a secondary install using wordpress.org download
Yeah, I should be able to do that.
Think I'll have to work on this later, though. Thanks for all your help! Least I know I'm not crazy. lol
Jan 4, 2016 21:34
Yeah, if you figure out the issue let me know cause now I'm curious xD
Good luck!
lol k, will do. And ty!
 
Conversation ended Jan 4, 2016 at 21:34.