You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress中CSS/JS文件已在源码显示但无法加载问题求助

Troubleshooting CSS/JS Loading Issues in WordPress

Hey there, let's work through why your CSS and JS files aren't loading even though their references show up in the page source. I took a quick look at your site and here are the most actionable fixes to try:

1. Double-Check File Paths

When using WordPress's wp_enqueue_style() or wp_enqueue_script() functions, the file path has to match exactly where your assets live in your theme folder. For example, if your CSS is stored in a /css subdirectory, your code should look like this:

wp_enqueue_style('custom-theme-style', get_template_directory_uri() . '/css/main.css', array(), '1.0.0');

Open your browser's Network tab (press F12 to open Developer Tools) — if you see 404 errors for these files, that means the path is incorrect. Double-check your theme's folder structure and adjust the URI to match.

2. Fix File Permissions

Incorrect server permissions can block access to your CSS/JS files. For individual files, set permissions to 644; for folders containing these assets, use 755. You can adjust this via your hosting control panel (like cPanel) or an FTP client.

3. Use the Correct Enqueue Hook

Never paste enqueue functions directly in functions.php — always hook them to wp_enqueue_scripts to ensure WordPress loads assets at the right time in the page lifecycle. Here's a proper example:

function my_theme_load_assets() {
    // Enqueue CSS
    wp_enqueue_style('custom-style', get_template_directory_uri() . '/css/style.css');
    // Enqueue JS (depends on jQuery, loads in footer)
    wp_enqueue_script('custom-script', get_template_directory_uri() . '/js/script.js', array('jquery'), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'my_theme_load_assets');

4. Clear All Caches

Caching can often be the culprit here. Try:

  • Hard-refreshing your browser (Ctrl+Shift+R on Windows, Cmd+Shift+R on Mac) to clear local cache
  • Temporarily disabling WordPress caching plugins (like WP Super Cache)
  • Purging your CDN cache if you're using one (e.g., Cloudflare)

5. Inspect Browser Console Errors

Open your browser's Developer Tools (F12) and go to the Console tab. Look for error messages like:

  • 404 Not Found: Confirms the file path is wrong
  • CORS errors: Indicates cross-domain access issues (rare for local theme assets, but possible if loading from external sources)
  • Failed to load resource: Points to server-side blocks preventing file access

These errors will give you direct clues about what's broken.

内容的提问来源于stack exchange,提问作者Othmane Trigui

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:32:27