如何在代码中排除Category ID 71的广告块追加逻辑?
Hey there! Let’s get this sorted out—excluding a specific category from your ad block logic is totally doable, and I’ll break it down for you step by step. I’ll assume you’re working with WordPress (since that’s the most common scenario for post category IDs; if you’re using a different CMS or custom setup, just tweak the code to match your environment!).
Step 1: Fetch the Current Post’s Category IDs
First, we need a reliable way to grab all category IDs linked to the current post. In WordPress, the get_the_category() function returns an array of category objects—we can extract just the IDs using wp_list_pluck().
Step 2: Add a Conditional Check to Skip Category 71
Wrap your existing ad block code in an if statement that verifies the current post doesn’t belong to category ID 71. Here’s a complete example:
// Get all category IDs for the current post $current_post_categories = wp_list_pluck(get_the_category(), 'term_id'); // Only display the ad block if category 71 is NOT present if (!in_array(71, $current_post_categories)) { // Paste your existing ad block code here echo '<div class="post-end-ad"> <!-- Your ad content (HTML/script) goes here --> </div>'; }
Key Notes:
- If your posts only ever belong to one category, you could simplify the check to
if (get_the_category()[0]->term_id != 71), but usingin_array()is safer for posts with multiple categories. - If you’re not using WordPress, the core logic stays the same:
- Get the category ID(s) of the current article.
- Add a condition that skips the ad block when the ID matches 71.
For a custom PHP setup, it might look like this:
// Assume $current_category_id holds the article's category ID if ($current_category_id !== 71) { // Your ad block code here }
Step 3: Test It Out
- Publish or preview a post in category 71: The ad block should not appear at the end.
- Test a post in any other category: The ad should show up as usual.
If it’s still not working, double-check that you’re using the correct category ID (sometimes IDs can be tricky to find—you can confirm it by editing the category in WordPress and checking the URL, which will have something like tag_ID=71).
内容的提问来源于stack exchange,提问作者Goob

