没有不值得去解决的问题,也没有不值得去学习的技术!

How to Configure AdSense Ads in the Sidebar of the WordPress 2025 Theme

这个修改解决了No slot size for availableWidth=0的错误,确保广告能够正确测量容器宽度并显示。

作者:

在

Adding sidebar ads in the WordPress 2025 theme used to be a challenge, but with the WPCode plugin, we can achieve precise ad placement. This article details how to successfully add AdSense display ads to the sidebar of the 2025 theme.

Problem Background

The WordPress 2025 theme uses a completely new block editor design, making the traditional PHP hook insertion method no longer applicable. In WPCode, because there is no fixed insertion location, we need to use shortcodes to achieve precise ad placement.

Solution: Using WPCode Shortcode

Step 1: Create a WPCode Ad Snippet

First, we need to create an ad code snippet in WPCode:
Create a new code snippet in WPCode, name it “[AdSense] Display Ad – Blog Homepage Sidebar Ad”, and paste the AdSense code into the code preview area.

Create a new code snippet in WPCode, name it "[AdSense] Display Ad - Blog Homepage Sidebar Ad", and paste the AdSense code into the code preview area.

Step 2: Set the Insertion Method to Shortcode

In the “Insert Method” section, select “Shortcode” mode: Copy the generated shortcode:

Step 3: Insert a Shortcode Block in the Sidebar

Go to the WordPress editor, add a “Shortcode” block in the sidebar area of the blog homepage template, and paste the copied shortcode into it:

Go to the WordPress editor, add a "Shortcode" block in the sidebar area of the blog homepage template, and paste the copied shortcode into it:

Issues Encountered and Solutions

Issue 1: Ad Not Displaying

Initially, although the ad code was present, the ad did not display.

Initially, although the ad code was present, the ad did not display.

By inspecting the source code, we found that WordPress automatically added the <p> tag, but this was not the root cause of the ad not displaying:

By inspecting the source code, we found that WordPress automatically added the <p> tag, but this was not the root cause of the ad not displaying:

The Real Problem: Console Error

Through the browser developer tools console, we found the actual error message:

Plaintext
Uncaught TagError: adsbygoogle.push() error: No slot size for availableWidth=0

This error indicates that AdSense could not measure the width of the ad container, so it could not determine what size ad to display.

Solution: Modify the Ad Code Style

In the WPCode snippet, we modified the ad code’s style by adding a width setting:

CSS
style="display:block; width:300px; max-width:100%;"
In the WPCode snippet, we modified the ad code's style by adding a width setting:


This modification resolved the No slot size for availableWidth=0 error, ensuring that the ad could correctly measure the container width and display.

This modification resolved the No slot size for availableWidth=0 error, ensuring that the ad could correctly measure the container width and display.

Final Result

Desktop Result

On desktop, the ad successfully displays in the sidebar.

Mobile Result

On mobile, the ad adapts to the screen width, although a horizontal scrollbar appears on the page. After disabling the ad code snippet, the horizontal scrollbar still appears. We initially confirmed that the horizontal scrollbar is not caused by the ad, so we will leave it as is for now.

On mobile, the ad adapts to the screen width, although a horizontal scrollbar appears on the page. After disabling the ad code snippet, the horizontal scrollbar still appears. We initially confirmed that the horizontal scrollbar is not caused by the ad, so we will leave it as is for now.

Summary

Using the WPCode shortcode method, we successfully added AdSense display ads to the sidebar of the WordPress 2025 theme. The key points are:

  1. Using WPCode’s shortcode mode instead of auto insertion
  2. Accurately pinpointing the root cause of the issue through the console error
  3. Setting appropriate width and maximum width limits for the ad
  4. Conducting thorough testing on mobile devices

This approach is not only applicable to AdSense but can also be extended to other scenarios where code needs to be inserted at specific locations.

系列导航

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系