WordPress.org

Ready to get started?Download WordPress

Forums

How to check if the menu links are top level ones? (6 posts)

  1. crisrealwp
    Member
    Posted 3 years ago #

    I'm editing post-template.php in WordPress. The function start_el echoes the menu. Here I want to check if the page is a top level page so I can add a new class called "top" only to the top level pages li. How do I check if the page/link is a top level one or not? I need only this as I can do the rest.

    I've tried with

    if ($page->post_parent) { $css_class_litop = 'top '; }
    but it doesn't work...

    (I've already changed in the same class with success the classes' names current_page_item and current_page_ancestor, current_page_parent to currentsince I use a custom jquery menu that uses this class.)

    Thanks a lot!

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    Why are you editing WordPress core files?

  3. crisrealwp
    Member
    Posted 3 years ago #

    This is just temporary to see if I'm hitting the "right spot". I'll move the changes/new functions to the functions.php file once it works.

  4. esmi
    Forum Moderator
    Posted 3 years ago #

    You would far better off designing your function to work with the default output than tinkering with odd core files. To test whether a given page is top-level, you could check to see if it has a parent ($post->parent, if I remember correctly).

  5. crisrealwp
    Member
    Posted 3 years ago #

    Thanks for the reply! Unfortunately, that doesn't work...

  6. crisrealwp
    Member
    Posted 3 years ago #

    I've managed to do it with jquery with help from stackoverflow. :D

Topic Closed

This topic has been closed to new replies.

About this Topic