As the warnings say, $post
is used but not defined, hence “Undefined variable $post
” and then ‘Attempt to read property “ID” on null’. It seems like the global $post
is in an erroneous location and should be before the if
statement:
global $post;
if ( is_singular() && wp_attachment_is_image ( $post->ID ) )
You may also wish to guard against the case of if $post
is empty, using the nullsafe operator:
if ( is_singular() && wp_attachment_is_image ( $post?->ID ) )
Or perhaps better yet, do the is_singular()
check before accessing $post
at all, since $post
should be defined if is_singular()
is true:
if ( is_singular() ) {
global $post;
if ( wp_attachment_is_image( $post->ID ) ) {
wp_enqueue_script( 'keyboard-image-navigation', get_stylesheet_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20120202' );
Related Posts:
- How to get the registered sidebar’s name by its id?
- Set the transport of the Customizer ‘header_image’ core setting to ‘postMessage’
- One button to change all settings in theme customizer?
- The best way to customize “nav-menu-template.php” to add if the ‘link_before’ is “checkbox”
- Enqueue less file not working in child theme?
- Design view breaking on Pages
- Execute only on a certain page
- How the functions in WP are called in tags
- Add a Second Menu to a theme that only support 1 menu
- Adding tawk.to code just before body tag on functions.php file
- Can’t load WP function into external function
- Set WordPress Default Template
- Where in PHP do I move title and meta (date) to bottom of each blog section?
- WordPress add post format support not working
- Compare the old get_theme_mod($name) to the new get_theme_mod($name) return value
- How do I link a button I created in theme customizer to a function?
- Is the “_s” on this `sprintf(__(‘Page %s’, ‘_s’), max($paged, $page))` just refer to a text domain?
- jQuery code not working when included in functions.php
- linking stylesheets and scripts with functions.php
- get_template_directory adding FTP root folders in urls
- How to: Conditionally Enqueue JS and Stylesheets, for Custom Post Type (Single and Archive Templates)
- Enqueing External JS on the remote server JS
- blank white page when post/update/change option etc
- Randomize Color Scheme Selection in Theme
- ‘Bones’ theme: Load stock scripts in footer instead of header?
- Enqueueing a code block from an options framework
- Displaying Slider in Front Page (home) using Customizr Theme
- How can I assign separate stylesheets to different pages?
- How to make sure relative URL works when site is not on root domain?
- call a string/function from database
- Some problems calling a function into sprintf() inside functions.php template file [closed]
- Dynamically switch file in get_template_directory_uri() | Function [closed]
- Uncaught TypeError: extract(): Argument #1 ($array) must be of type array, null given
- remove_action from parent theme using child theme functions.php
- File from parent theme imported to child theme doesn’t work – any ideas?
- How to use data URL in WordPress?
- How to change order inside foreach using wp_enqueue_script?
- How to override theme’s public static function inside of a trait?
- Enqueue sripts and styles only if function is called
- how to use 2 index.php file One for mobile and one for desktop
- Menu to the right of screen on desktop using Bootstrap 4
- How can I print out a single stylesheet or javascript link?
- Adding Gravity Form With if(is_page) Is Not Working
- Errors while Loading Most Recent Version of jQuery
- Need help with Deprecated: Non-static error when update PHP 7.4 -> 8.1 with Dyad 2 theme
- Warning: filemtime(): stat failed for wp_
- a problem in class in class-wp-hook.php
- Error Warning: Declaration of description_walker::start_el after PHP upgrade [duplicate]
- Mega Menu Walker
- Autoloading in Child Theme
- How to add a class to the attachment images
- How to export/import theme customizer settings?
- Activate different theme for temporary preview
- add_rewrite_rule with bottom priority doesn’t handle the WordPress pages
- Change logo based on incoming domain
- How do themes render caption texts in extended markup (e.g. “wp-caption” paragraphs)
- Count the number of times the search form template gets included on a page
- PHP code inside shortcodes
- Remove meta description on certain pages
- Image media upload metabox
- Woocommerce related product text
- esc_html_e() is not translating string in wordpress [closed]
- Limit 1 global comment per minute
- Remove echo from shortcode
- Copyright info change – Corporate Plus Theme PHP [closed]
- Inject class in body when particular page template is used
- Echo a function inside a function
- Use WordPress function in php file
- wp_force_remove_style’ not found
- Pagination on Custom Post
- how to save checkbox data for custom setting?
- How to keep the capability of users and disable Gutenberg editor in WordPress?
- How to add aria role and schema markup to custom walker container
- Remove submenu item from list
- Customize field names in backend profile edit page through function.php [duplicate]
- Insert content into head tag with function
- Getting text from custom field from customizer
- Time Delay a URL Redirect for Specific Page
- Execute function only for specific user roles
- Include User ID in iFrame URL
- Run a sql (update) after 12 hours after the user login. Woocommerce users
- Wp_Schedule_Event every few minutes doesn’t work
- How to center all text body in single.php at once?
- Access WP files on “server 1”, from “server 2” – using wp-load on an external website
- How to include a function in a template with template tag
- wordpress all post filter by year
- Pulling a variable into the wp_nav_menu function
- Create a custom theme-specific page, invisible in the admin-panel?
- Chosen Select jquery Not Working in Plugin
- Problem with function.php.. like
- Echo get_option displays as text
- How to extract URLs from wordpress taxonomies
- Warning: Illegal string offset ‘post’ and ‘page’
- Remove content links (internal and external), but exclude post at specific categories
- Best way to delete/don’t load a .php file?
- Get collected weight ordered per shipment method
- Add adjacent post function inside custom recent post function
- Customize Notification Email Sent to New User When Manually Created by Site Administrator
- Woocommerce Select Option Popup [closed]
- Function extension