kraftner is probably right but I recommand you to stop using global $post. I mean it’s not bad but there are often issues with this big array that can be modified by almost everything running on the wp install.
So to me it’s not very safe. I prefer using get_queried_object()
and it works quite the same way :
$parent_ID = get_queried_object()->post_parent;
It retrieves data according to the current page. It uses the global $wp_query
instead. Be careful here we try to get post_parent property so don’t run this code on a category page you’d get a warning.
Related Posts:
- Custom Nav Walker Displaying Values in Sub Menu
- Custom Nav Walker to show siblings and children of current branch?
- Create self-populating menu and add extra divs to the menu layout
- Find the Children of a Page then Echo it as a Bulleted List of Links (menu)
- Single-level menu option? Another way?
- Admin: sub menu doesnt display under apperance when activate my themes
- WordPress custom menu can’t show 3. level submenu
- Theme: dropdown hover menu not showing up in IE/Edge
- Show all menu levels except top level. How to exclude top level of a menu?
- WordPress menu walker – Get parent item text inside end_lvl function
- WordPress doesn’t recognize submenus as so I can’t style them properly
- Main Menu Theme Different Output Print
- Add wrapper for a specific sub-menu
- Menu items description? Custom Walker for wp_nav_menu()
- How to add posts to custom menus?
- wp_nav_menu(), how to change class?
- Display Menu Name using wp_nav_menu
- WordPress API Menu/Submenu Order
- How can I display a menu on certain pages only?
- Add custom classes to anchor in wp_nav_menu
- Add Caret to Menu Items with Sub-Menus in WordPress Theme
- How to limit wordpress menu depth in admin panel
- wp_nav_menu sort order?
- How to determine if custom menu is active?
- How do I check if a menu exists?
- How to use logout function on custom menu link?
- Any walker causes blank menu?
- How to display wp_nav_menu() conditionally only when not on a 404 page?
- How to add menu support to a theme?
- Problem with registering menus – What to do when other solutions aren’t working?
- Add a #hash to the links in my custom menu
- How can i change menu link when page content empty?
- Select menu on browser resize
- Extend walker – navigation, adding data attribute to a tag
- How to add (css) classes to only one wp_nav_menu()?
- Get last modified date for menu link
- Replace Home with image link inside custom header menu
- Displaying wp menus by name without using theme locations
- How to add nofollow to wp_nav_menu
- How to highlight the current page in the nav menu?
- How do I add a post to a menu
- how to change WordPress menu markup/classes
- Setting a Default ‘Theme Location’ When Creating a Menu
- WordPress nav_menu_css_class theme filter is not being called
- Best practices: Custom theme sidebar menu – hardcode or widget?
- How do I highlight the menu for a child page?
- how to set more than one menus to the same theme location
- CSS won’t style output of wp_nav_menu() correctly
- Where is definied the theme location for the main menu in a WordPress template?
- how to wrap tag in tag html to wordpress menu conversion
- Second menu not showing
- How to remove class attributes from custom menu markup?
- 2 differen’t menus and it’s using same items in both
- menu behavior is not as expected
- Is there an action for save_menu and/or update_menu?
- Set default screen option for appearance -> menus
- Part of Menu Item Hidden on Header Menu
- wp_nav_menu add something before closing container and after last list item
- Mark menu item as current-menu-item for category
- which is the best way to customize nav-menu-template.php?
- Create wordpress header with custom logo in between the menu splitting the menu into two equal parts [closed]
- WordPress navbar with logo in middle [closed]
- My Admin bar covers my sticky navbar [closed]
- WordPress is adding margins and padding to my custom menu?
- Menu item added in wp_nav_menu_items filter is never highlighted
- WordPress empty Nav Menu error
- multisite: global menu containing network site links?
- How to make theme elements customizable in wordpress?
- Primary navigation menu & footer not showing in category / tag page
- Using a page as home, how do I prevent nav from setting both home and page links active?
- Problem with empty class attributes when running a filter on nav_menu_css_class
- How to append to menu items selectively
- Custom Nav Walker Trouble
- How to set the currently active page in the menu including parents
- Issue on Adding A Class to Bootstrap Navbar Walker Dropdown
- Show woocommerce categories on my theme menu styling
- Where am I doing wrong in my theme menu?
- Custom Navigation build using wp_nav_menu and walker
- Override customizer values on a per-menu base
- Why does has_nav_menu return false when get_registered_nav_menus shows the opposite?
- Recommended method for updating/adding menus when adding new theme
- Child Theme header1.php file not overwriting parent theme’s header1.php file
- Why wp_nav_menu() doesnt show the proper meny when invoked two consecutive times?
- How to develop a menu like wwe.com?
- 3 Level Deep Navigation Menu Not Showing All Levels
- Why are some of my menu items not displaying on my menu?
- How to Create custom xml file for demo content for custom theme?
- How should I use starter content utility to show some default menus?
- Add child pages to submenu automatically
- How to create and populate with few links a menu in child theme functions.php?
- Second menu has the first menu ID
- How to add menù section to my WordPress template?
- wp_nav_menu and its fallback
- Thesis -style Navigation
- Use a filter on menu items that have children
- Theme development – Automatically menu creation
- How can i make multilevel menu in wordpress theme with bootstrap
- how do i develop a global nav and local nav to be wordpress dynamic?
- Theme development: How to add CSS classes to menu items?
- Navbar- disable “onClick” drop-down