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
- Image as Sales Badge
- how to make separate field in wordpress user_data_
- Function not working at one place
- jQuery not defined
- Displaying the Month and Year that a page was Created?
- Changing custom logo link
- Add a class to the anchor tag on HTML5 gallery
- Conditional Header in wordpress
- Remove meta description on certain pages
- Menu — How to add “current-menu-grand-ancestor” css class
- Use menu link or onClick to set a variable
- Swapping wp_dropdown_categories function with wp_category_checklist
- functions.php is being included twice, creating PHP fatal errors
- Hide Hamburger Menu On Specific Page (Front Page/Home Page)
- Automatically remove a canceled order in Woocommerce
- Display fields as values in array from external SQL DB
- Only Show Link If div Exists
- How to put a variable in a instance in the widget
- Create another “Display Site Title and Tagline” checkbox, “Header Text Color” setting and control
- Trying to establish connection to External Database
- add_action shortcut?
- Passing the custom field values in the wp_get_current_user array function
- Adding custom directory and PHP files in wordpress
- Using fwrite() and “a” appends multiple times instead of once
- How to output a PHP file values by shortcode?
- Showing content from one page on another
- How to style options page in dashboard?
- How to insert a single row to WP database table?
- Get date numerical and separate?
- WSoD being caused by this piece of code
- Converting HTML Template to WordPress Theme
- Set the background to a default image if there isn’t a specified “featured image”
- How to Arrange PHP Files to Allow for Multiple Types of Pages?
- Add a variable in functions.php and call it in single.php
- How to access global variable $menu inside a class function
- Show full posts in archive
- Calling directories to load in wordpress
- Can I prevent the user from adding more than two levels deep of terms inside of a taxonomy metabox?
- Image media upload metabox
- How to remove the cufon script from Dzonia Lite theme [closed]
- How to remove or change the “W” icon in “My Sites”?
- get a simple array of all of the term names that exist in all taxonomies
- Login/logout in header
- How to display posts by current user in a drop down
- Logged in user ID as post ID
- How does WordPress insert Paragraphs in posts
- WordPress Access allowed to fewer pages till user logs in
- Linking text within textarea of custom meta box
- WordPress: Access a plugin from within a theme
- Show css depending on activity type in BuddyPress activity-loop [closed]
- only show container with next/prev links if they exist?
- How to combine wordpress_logged_in cookies in one cookie?
- Populate dropdown with Child Pages based on Parent Page chosen