首页 问答 正文

如何在wordpress主题中添加自定义广告位?

注册会员 / 悠然自学 / 2023-06-11/ 浏览 240 次

可以通过以下步骤在WordPress主题中添加自定义广告位:

  1. 在主题文件夹中创建一个名为“functions.php”的文件。
  2. 在“functions.php”文件中添加以下代码:
function custom_ad_widgets_init() {
    register_sidebar( array(
        'name' => __( 'Custom Ad Widget', 'custom-ad-widget' ),
        'id' => 'custom-ad-widget',
        'description' => __( 'Custom Ad Widget Area', 'custom-ad-widget' ),
        'before_widget' => '<div class="custom-ad">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
}
add_action( 'widgets_init', 'custom_ad_widgets_init' ); 

上述代码将创建一个名为“Custom Ad Widget”的小工具区域,并在小工具前后添加适当的HTML标记。

  1. 在主题文件夹中打开“sidebar.php”文件,并在想要放置广告的地方添加以下代码:
<?php if ( is_active_sidebar( 'custom-ad-widget' ) ) : ?>
    <?php dynamic_sidebar( 'custom-ad-widget' ); ?>
<?php endif; ?> 

上述代码将检查是否为名为“Custom Ad Widget”的小部件区域激活,如果是,则在相应位置显示该区域中的小部件。

示例代码已提供,无需进一步说明。

大家谈论
    我的见解