不同分类设置专属Logo问题:AF-Gaming分类及文章Logo替换失败求助
Let’s break down why your code isn’t working and fix it step by step:
1. Critical Syntax Error
First off, you’ve got a typo in your PHP tag: <? Php } should be <?php } (lowercase "php"). PHP doesn’t recognize mixed-case opening tags like that, which is likely causing your code to fail entirely.
2. Wrong Placement in functions.php
You can’t drop raw HTML output code directly into functions.php—that file is meant for defining functions, hooks, and logic, not printing content directly. Doing this will either output the image at the top of your site (breaking page structure) or prevent it from showing up in the correct logo position.
Correct Approach:
- Step 1: Create a reusable function in
functions.php
Replace your current code with this cleaned-up, secure version:function custom_category_specific_logo() { // Define your logo URLs $gaming_logo = 'https://afdigitale.it/wp-content/uploads/2018/03/AF-Gaming.png'; $default_logo = 'https://afdigitale.it/wp-content/uploads/AF-Digitale-logo.png'; // Check if we're on the AF-Gaming category archive OR a post in that category if (is_category(6933) || in_category(6933)) { $logo_url = $gaming_logo; } else { $logo_url = $default_logo; } // Output the logo with proper URL escaping for security echo '<img src="' . esc_url($logo_url) . '" alt="Site Logo" />'; } - Step 2: Replace your existing logo in
header.php
Open your theme’sheader.phpfile, find the line where your current logo is printed, and replace that line with:<?php custom_category_specific_logo(); ?>
3. Verify Category ID is Correct
Double-check that 6933 is the right ID for your af-gaming category:
- Go to Posts → Categories in your WordPress admin
- Hover over the "AF-Gaming" category and look at the URL in your browser’s status bar—you’ll see something like
tag_ID=6933 - If the ID doesn’t match, update the number in the code above
4. Cover Both Category Archives & Individual Posts
Your original code only checks for the category archive page (is_category()). We added in_category(6933) to the condition so the gaming logo will also show on individual posts in the AF-Gaming category—adjust this if you only want the logo change on the archive page.
5. Clear Cache
If you’ve made these changes and still don’t see the logo update, clear:
- Your WordPress caching plugin (if you use one)
- Your server’s cache (like Cloudflare, if enabled)
- Your browser’s cache (Ctrl+Shift+R on Windows, Cmd+Shift+R on Mac)
Bonus: Use WordPress Native Functions (More Reliable)
Instead of hardcoding image URLs, use WordPress’s media functions to get logo URLs dynamically—this prevents broken links if you ever move your files:
- Go to Media → Library in your admin, find your logos, and note their attachment IDs (look at the URL when editing the image:
post=XXX) - Update the function to use
wp_get_attachment_image_src():function custom_category_specific_logo() { // Replace XXX and YYY with your actual attachment IDs $gaming_logo_data = wp_get_attachment_image_src(XXX, 'full'); $default_logo_data = wp_get_attachment_image_src(YYY, 'full'); $gaming_logo = $gaming_logo_data ? $gaming_logo_data[0] : $default_logo_data[0]; $default_logo = $default_logo_data ? $default_logo_data[0] : ''; if (is_category(6933) || in_category(6933)) { $logo_url = $gaming_logo; } else { $logo_url = $default_logo; } if ($logo_url) { echo '<img src="' . esc_url($logo_url) . '" alt="Site Logo" />'; } }
内容的提问来源于stack exchange,提问作者mic

