WooCommerce条件标签失效求助:混合WP/WC标签的WC页面判断不生效
Hey there! Let's tackle why your WooCommerce conditional checks aren't working alongside the logged-in user logic. I've run into this exact issue before, so let's break it down step by step.
First, Let's Clarify the Core Logic
Your goal is: Don't load the specified code if the user is logged in OR they're on any WooCommerce page. That means we only load the code when the user is not logged in AND not on a WooCommerce page.
The Most Common Culprit: Hook Timing
WooCommerce's conditional tags (like is_woocommerce(), is_checkout()) won't work if you call them too early in WordPress's load sequence. For example, hooks like after_setup_theme run before WooCommerce initializes, so those tags will always return false.
The Fix: Use the Right Hook & Correct Condition
Let's use a hook that fires after WooCommerce is fully initialized, like wp_enqueue_scripts (great for loading scripts/styles) or template_redirect (if you're modifying content output).
Here's a working example you can drop into your theme's functions.php file:
add_action('wp_enqueue_scripts', 'conditional_load_custom_code'); function conditional_load_custom_code() { // Check if user is logged in OR we're on any WooCommerce page if (is_user_logged_in() || is_woocommerce()) { // Skip loading our code return; } // This is where your code to load goes - example below: wp_enqueue_script( 'my-custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), '1.0.0', true ); }
If You Need More Specific WooCommerce Page Checks
If is_woocommerce() (which covers all core WooCommerce pages: shop, product, cart, checkout, account) isn't enough, you can combine specific tags like this:
// Check for any of these WooCommerce pages $is_woocommerce_page = is_woocommerce() || is_cart() || is_checkout() || is_account_page() || is_product() || is_product_category(); if (is_user_logged_in() || $is_woocommerce_page) { return; }
Debugging Tips to Verify
If it's still not working, add a quick debug check to see what the conditional tags are returning:
- Drop this into your theme's
header.php(inside the<body>tag) temporarily:<?php echo "Logged in: " . (is_user_logged_in() ? 'Yes' : 'No') . "<br>"; echo "Is WooCommerce page: " . (is_woocommerce() ? 'Yes' : 'No') . "<br>"; ?> - Visit a WooCommerce page (like your cart or shop) and check the output. If
Is WooCommerce pagesaysNo, then WooCommerce isn't properly initialized where you're calling the tag.
Edge Case: Custom Pages with WooCommerce Shortcodes
If you have custom pages using WooCommerce shortcodes (like [woocommerce_cart]), is_woocommerce() won't detect them. For those, you can add a check to see if the post contains a WooCommerce shortcode:
global $post; $has_woocommerce_shortcode = isset($post) && has_shortcode($post->post_content, 'woocommerce'); if (is_user_logged_in() || is_woocommerce() || $has_woocommerce_shortcode) { return; }
That should cover most scenarios! Let me know if you run into any snags.
内容的提问来源于stack exchange,提问作者dungey_140

