This is because you have functions that work only inside of the WordPress Loop like get_the_id()
. This will return the correct value inside the WP Loop in template files, but when called in other places does not return the correct value and check fails.
See this for more information on the WordPress loop
https://codex.wordpress.org/The_Loop
When outside the loop you can either use the global $post
object to get the id like this
global $post
$id = $post->ID;
Alternately you can try this
$id = get_queried_object_id();
Related Posts:
- Cleaning up WordPress to improve performance?
- List of all theme customizer control types?
- Modify previous and next text from pagination links
- How can I modify the permalink via a filter?
- Shortcode putting html such as
- Calling a method from functions.php on a click of a button
- How to redirect users to custom lostpassword page?
- The best way to customize “nav-menu-template.php” to add if the ‘link_before’ is “checkbox”
- How to change featured content to a different tag in WordPress Twenty Fourteen?
- Can I write ‘RewriteCond’ using ‘functions.php’?
- How do I create comment-reply-button using element not
- WordPress menu deletes when trying to add a hook
- Overriding a theme redux file in child theme
- Validate a users email address when using gmail to register
- Output a WooCommerce product custom field in WooCommerce using get_post_meta()
- How to check If Oembed is empty or not
- Making Quote Plugin more efficient
- allow subscribers to see private posts without plug in
- Displaying the Month and Year that a page was Created?
- Only Show Link If div Exists
- How to access global variable $menu inside a class function
- Change the WP Video Shortcode Output
- How to pick the default selected value in wordpress dropdown?
- Using WP-API and SSE not authenticating user ID
- Validate functions before inserting then into functions.php
- How to retrieve current wordpress profile page URL?
- Search not showing all results
- WordPress Quick Question . How to Get Parent Link in Submenu in My Code
- How to Reference/Echo Variable from Another PHP Function
- Detecting classes, adding widgets, and adding divs in with a Nav Walker
- Is the “_s” on this `sprintf(__(‘Page %s’, ‘_s’), max($paged, $page))` just refer to a text domain?
- Exclude category
- How can I make an object available through the entire site?
- WordPress admin-ajax.php
- What exactly do this function declared into functions.php file of a WP theme?
- How do I modify the custom footer text and get theme version number to show in wordpress admin?
- How to use html inside a functions.php code?
- Nested “do” and “add” Actions is possible?
- Display Data in Table from External Database in WP using Shortcodes
- How to check if a Customizer setting is set?
- linking stylesheets and scripts with functions.php
- Show ACF field with link to ultimate member profile/WordPress user profile below the post (single post layout)
- Remove item in Checkout
- How to get current post category details inside “loop”?
- Co-Authors Plus: How do I get all authors with a query?
- How to reference PHP in Javascript
- Only load certain artists on this page
- Create special button on WP Tiny MCE Posts Editor for Shortcodes
- Get the alt text of the image in a array
- making php value numeric
- Make table disappear when fields are left blank?
- How to Redirect Buddypress Directories To Login Page For Non Logged in Users
- Woocommerce order status payment
- How to define a rule in functions.php that ONLY disables the default wp functionality that undesirably changes ‘&’ to ‘#038;’?
- Unzip file in functions.php, and add it to cron
- Add Block Before Entry Title Using PHP
- How to assign page id with array in page_scheduled_event
- Display logged in user name and lastname on page
- How to Add a cutsom slug to my custom author role
- Dynamically populate parameter in Gravity Forms using PHP
- Store ajax data in PHP variable
- comment_post (if comment is approved OR $comment_approved === 1) not working?
- Cannot find the php_ini configuration file to find my error log to see why my code is not working
- How to output the taxonomies that are assigned to each custom post?
- Having trouble creating two shortcodes, one for logged in user and one for visitors
- How can i create a function tag in my plugin
- How to escape echo for input tag?
- Including user data in “new user notification email”
- Display current user metadata on WordPress page
- Reload page with a different shortcode when a user selects from a dropdown
- Refresh page after login with litespeed cache
- Return newly created category by code
- WordPress Ajax filter: Create two loops for different output styles?
- Woocommerce Add custom “Add to cart button” in single page with tags
- Hook into all password resets in WordPress and get password before hashing?
- How to call a function from functions.php with ajax?
- remove_action() not working in page template – Genesis
- How to sort (orderby) a query done by a template function before the ‘foreach’ loop?
- Dropdown menu on custom menu
- Showing wordpress latest post thumbnails in slider with auto increment
- What is wrong in this code?
- How can the searchform.php know if it’s used on a registered sidebar id ‘sidebar-1’ or ‘sidebar-2’?
- WordPress get_post_meta issue
- Woocommerce Price Text
- Limit Taxonomy Parents
- Modify php code to pass a page id as a parameter in order to create a breadcrumb
- Show custom default avatar instead of gravatar
- WordPress 2014 Theme – Featured Content with multiple tags?
- How to add button to top of theme customizer?
- ajax form function error
- Display custom meta box in my template file
- Woocommerce – Shipping tax class based on cart items not using the highest tax available
- How to display the date under the post title?
- Simple Probléme in wordpress
- Options.php loop won’t show!
- Contact form with dynamic dropdown and filter
- Critical Error customiser
- Alternative to php echo within code
- Add multiple HTML attributes to an Elementor button
- /page/2/ redirect to 404