通知图标 加载中...
子比主题 – 文章页侧边广告小工具-子比美化备份交流社区-盒子内部事务-设计盒子

子比主题 – 文章页侧边广告小工具

原文地址:https://www.zibll.com/forum-post/38989.html

只整理,未做测试

20250928085108425-799f6687c9029d8b9c53f4bfd4dbcc47_Snipaste_2025-09-19_18-07-24

 

20250928085120154-175b82ee62ada24a9ae94a0f3bfd04d4_Snipaste_2025-09-19_18-09-55

 

以下代码添加至主题目录下func.php文件中

/**
 * HEIMAO - 侧边广告卡片小工具
 */
if (!class_exists('ZB_Side_Ad_Widget')) {
    class ZB_Side_Ad_Widget extends WP_Widget {
        public function __construct() {
            $widget_ops = array(
                'description' => '广告卡片样式,支持自定义图片、链接、标题、标签、QQ号',
            );
            parent::__construct('zb_side_ad_widget', 'HEIMAO - 侧边广告卡片', $widget_ops);
        }

        // 后台设置表单
        public function form($instance) {
            $instance = wp_parse_args((array)$instance, array(
                'ad_image' => 'https://img.520ak.com/i/2025/09/19/Snipaste_2025-09-19_16-20-49.webp',
                'ad_link'  => '#',
                'ad_title' => '云产品精选,云服务器、数据库等优惠福利!',
                'ad_tags'  => 'AD,腾讯云',
                'ad_qq'    => '88888888',
            ));
            $ad_image = esc_attr($instance['ad_image']);
            $ad_link  = esc_attr($instance['ad_link']);
            $ad_title = esc_attr($instance['ad_title']);
            $ad_tags  = esc_attr($instance['ad_tags']);
            $ad_qq    = esc_attr($instance['ad_qq']);

            // 图片地址输入
            echo '<p><label for="' . $this->get_field_id('ad_image') . '">广告图片地址:</label>';
            echo '<input class="widefat" id="' . $this->get_field_id('ad_image') . '" name="' . $this->get_field_name('ad_image') . '" type="text" value="' . $ad_image . '" /></p>';

            // 跳转链接输入
            echo '<p><label for="' . $this->get_field_id('ad_link') . '">点击跳转链接:</label>';
            echo '<input class="widefat" id="' . $this->get_field_id('ad_link') . '" name="' . $this->get_field_name('ad_link') . '" type="text" value="' . $ad_link . '" /></p>';

            // 标题输入
            echo '<p><label for="' . $this->get_field_id('ad_title') . '">图片下方标题:</label>';
            echo '<input class="widefat" id="' . $this->get_field_id('ad_title') . '" name="' . $this->get_field_name('ad_title') . '" type="text" value="' . $ad_title . '" /></p>';

            // 标签输入(多标签用逗号分隔)
            echo '<p><label for="' . $this->get_field_id('ad_tags') . '">标签(逗号分隔,如“AD,腾讯云”):</label>';
            echo '<input class="widefat" id="' . $this->get_field_id('ad_tags') . '" name="' . $this->get_field_name('ad_tags') . '" type="text" value="' . $ad_tags . '" /></p>';

            // QQ号输入
            echo '<p><label for="' . $this->get_field_id('ad_qq') . '">QQ号码:</label>';
            echo '<input class="widefat" id="' . $this->get_field_id('ad_qq') . '" name="' . $this->get_field_name('ad_qq') . '" type="text" value="' . $ad_qq . '" /></p>';
        }

        // 保存后台设置
        public function update($new_instance, $old_instance) {
            $instance = $old_instance;
            $instance['ad_image'] = sanitize_text_field($new_instance['ad_image']);
            $instance['ad_link']  = sanitize_text_field($new_instance['ad_link']);
            $instance['ad_title'] = sanitize_text_field($new_instance['ad_title']);
            $instance['ad_tags']  = sanitize_text_field($new_instance['ad_tags']);
            $instance['ad_qq']    = sanitize_text_field($new_instance['ad_qq']);
            return $instance;
        }

        // 前台渲染广告卡片(移除默认外层容器 + 修复显示不完整+样式增强)
        public function widget($args, $instance) {
            // 核心修改:清空/替换默认的 before_widget(移除 zib-widget widget_zb_side_ad_widget 容器)
            // 原 $args['before_widget'] 包含 <div class="zib-widget widget_zb_side_ad_widget">,此处直接替换为空或自定义结构
            $args['before_widget'] = '';
            // 同步清空 after_widget(避免残留闭合标签)
            $args['after_widget'] = '';

            // 提取并容错处理参数
            $ad_image = !empty($instance['ad_image']) ? $instance['ad_image'] : 'https://img.520ak.com/i/2025/09/19/Snipaste_2025-09-19_16-20-49.webp';
            $ad_link  = !empty($instance['ad_link']) ? $instance['ad_link'] : '#';
            $ad_title = !empty($instance['ad_title']) ? $instance['ad_title'] : '云产品精选,云服务器、数据库等优惠福利!';
            $ad_tags  = !empty($instance['ad_tags']) ? $instance['ad_tags'] : 'AD,腾讯云';
            $ad_qq    = !empty($instance['ad_qq']) ? $instance['ad_qq'] : '88888888';

            // 分割并清理标签
            $tags_array = explode(',', $ad_tags);
            $tags_array = array_map('trim', $tags_array);

            // 输出小工具(已移除默认外层容器)
            echo $args['before_widget'];
            ?>
                <div class="posts-item card ajax-item" style="width: 100%; margin-bottom: 20px;">
                    <!-- 图片容器:自适应 + 防溢出 -->
                    <div class="item-thumbnail" style="width: 100%;">
                        <a href="<?php echo esc_url($ad_link); ?>" target="_blank">
                            <img 
                                src="<?php echo esc_url($ad_image); ?>" 
                                alt="广告图片" 
                                class="fit-cover radius8" 
                                style="max-width: 100%; height: auto; display: block;" 
                            />
                        </a>
                    </div>

                    <!-- 文字内容容器:内边距 + 换行 -->
                    <div class="item-body" style="width: 100%; padding: 10px;">
                        <!-- 标题:自动换行 + 行高 -->
                        <h2 class="item-heading" style="white-space: normal; line-height: 1.4; margin-top: 0; font-size: 16px;">
                            <a href="<?php echo esc_url($ad_link); ?>" rel="nofollow" target="_blank">
                                <?php echo esc_html($ad_title); ?>
                            </a>
                        </h2>

                        <!-- 标签栏:横向滚动 + 间距 -->
                        <div class="item-tags scroll-x no-scrollbar mb6" style="display: flex; overflow-x: auto; padding-bottom: 5px;">
                            <?php foreach ($tags_array as $tag) : ?>
                                <?php if ($tag === 'AD') : // “AD”标签渐变样式 ?>
                                    <a 
                                        class="but c-blue" 
                                        title="广告" 
                                        style="background: linear-gradient(135deg,#ff5473 10%,#fb6bea 100%); color: #fff; margin-right: 5px; padding: 2px 8px; border-radius: 4px;"
                                    >
                                        <i class="fa fa-desktop" aria-hidden="true"></i><?php echo esc_html($tag); ?>
                                    </a>
                                <?php else : // 普通标签样式 ?>
                                    <a 
                                        class="but" 
                                        style="margin-right: 5px; padding: 2px 8px; border-radius: 4px;"
                                    >
                                        <?php echo esc_html($tag); ?>
                                    </a>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </div>

                        <!-- 底部信息:对齐 + 间距 -->
                        <div class="item-meta muted-2-color flex jsb ac" style="align-items: center;">
                            <item class="meta-author flex ac" style="display: flex; align-items: center;">
                                <span class="avatar-mini" style="margin-right: 5px;">
                                    <img 
                                        alt="广告头像" 
                                        src="https://img.520ak.com/i/2025/09/19/Ad.png" 
                                        class="avatar avatar-id-1" 
                                        style="width: 25px; height: 25px; border-radius: 50%; margin-top: 6px;" />
                                </span>
                                <span title="广告联系" class="ml6" style="font-size: 14px;">广告联系</span>
                            </item>
                            <div class="meta-right" style="display: flex; align-items: center;">
                                <item class="meta-like" style="border: 1px solid #673ab77d; border-radius: 9px; padding: 2px 8px; font-size: 14px;">
                                    <a 
                                        href="javascript:;" 
                                        data-toggle="tooltip" 
                                        title="复制QQ" 
                                        data-clipboard-text="<?php echo esc_html($ad_qq); ?>"
                                    >
                                        <svg t="1758274565772" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="14311" width="64" height="64"><path d="M511.037 986.94c-85.502 0-163.986-26.686-214.517-66.544-25.66 7.149-58.486 18.655-79.202 32.921-17.725 12.202-15.516 24.647-12.32 29.67 14.027 22.069 240.622 14.092 306.04 7.219v-3.265z" fill="#FAAD08" p-id="14312"></path><path d="M495.627 986.94c85.501 0 163.986-26.686 214.518-66.544 25.66 7.149 58.485 18.655 79.203 32.921 17.724 12.202 15.512 24.647 12.32 29.67-14.027 22.069-240.623 14.092-306.042 7.219v-3.265z" fill="#FAAD08" p-id="14313"></path><path d="M496.137 472.026c140.73-0.935 253.514-27.502 291.73-37.696 9.11-2.432 13.984-6.789 13.984-6.789 0.032-1.25 0.578-22.348 0.578-33.232 0-183.287-88.695-367.458-306.812-367.47C277.5 26.851 188.8 211.021 188.8 394.31c0 10.884 0.55 31.982 0.583 33.232 0 0 3.965 4.076 11.231 6.048 35.283 9.579 150.19 37.482 294.485 38.437h1.037zM883.501 626.967c-8.66-27.825-20.484-60.273-32.455-91.434 0 0-6.886-0.848-10.366 0.158-107.424 31.152-237.624 51.006-336.845 49.808h-1.026c-98.664 1.186-227.982-18.44-335.044-49.288-4.09-1.176-12.168-0.677-12.168-0.677-11.97 31.16-23.793 63.608-32.453 91.433-41.3 132.679-27.92 187.587-17.731 188.818 21.862 2.638 85.099-99.88 85.099-99.88 0 104.17 94.212 264.125 309.947 265.596a765.877 765.877 0 0 1 5.725 0c215.738-1.471 309.947-161.424 309.947-265.595 0 0 63.236 102.519 85.102 99.88 10.186-1.231 23.566-56.14-17.732-188.819" p-id="14314"></path><path d="M429.208 303.911c-29.76 1.323-55.195-32.113-56.79-74.62-1.618-42.535 21.183-78.087 50.95-79.417 29.732-1.305 55.149 32.116 56.765 74.64 1.629 42.535-21.177 78.08-50.925 79.397m220.448-74.62c-1.593 42.507-27.03 75.941-56.79 74.62-29.746-1.32-52.553-36.862-50.924-79.397 1.614-42.526 27.03-75.948 56.764-74.639 29.77 1.33 52.57 36.881 50.951 79.417" fill="#FFFFFF" p-id="14315"></path><path d="M695.405 359.069c-7.81-18.783-86.466-39.709-183.843-39.709h-1.045c-97.376 0-176.033 20.926-183.842 39.709a6.66 6.66 0 0 0-0.57 2.672c0 1.353 0.418 2.575 1.072 3.612 6.58 10.416 93.924 61.885 183.341 61.885h1.045c89.416 0 176.758-51.466 183.34-61.883a6.775 6.775 0 0 0 1.069-3.622 6.66 6.66 0 0 0-0.567-2.664" fill="#FAAD08" p-id="14316"></path><path d="M464.674 239.335c1.344 16.946-7.87 32-20.55 33.645-12.701 1.647-24.074-10.755-25.426-27.71-1.326-16.954 7.873-32.008 20.534-33.64 12.722-1.652 24.114 10.76 25.442 27.705m77.97 8.464c2.702-4.392 21.149-27.488 59.328-19.078 10.028 2.208 14.667 5.457 15.646 6.737 1.445 1.888 1.84 4.576 0.375 8.196-2.903 7.174-8.894 6.979-12.217 5.575-2.144-0.907-28.736-16.948-53.232 6.99-1.685 1.648-4.7 2.212-7.558 0.258-2.856-1.956-4.038-5.923-2.342-8.678" p-id="14317"></path><path d="M503.821 589.328h-1.031c-67.806 0.802-150.022-8.004-229.638-23.381-6.817 38.68-10.934 87.294-7.399 145.275 8.928 146.543 97.728 238.652 234.793 239.996h5.57c137.065-1.344 225.865-93.453 234.796-239.996 3.535-57.986-0.584-106.6-7.403-145.283-79.631 15.385-161.861 24.196-229.688 23.389" fill="#FFFFFF" p-id="14318"></path><path d="M310.693 581.35v146.633s69.287 13.552 138.7 4.17V596.897c-43.974-2.413-91.4-7.79-138.7-15.546" fill="#EB1C26" p-id="14319"></path><path d="M806.504 427.238s-130.112 43.08-302.66 44.309h-1.025c-172.264-1.224-302.217-44.161-302.66-44.309L156.58 541.321c108.998 34.464 244.093 56.677 346.238 55.387l1.024-0.002c102.152 1.297 237.226-20.917 346.24-55.385l-43.579-114.083z" fill="#EB1C26" p-id="14320"></path></svg><?php echo esc_html($ad_qq); ?>
                                    </a>
                                </item>
                            </div>
                        </div>
                    </div>
                </div>
            <?php
            echo $args['after_widget'];
        }
    }

    // 注册小工具
    function zb_register_side_ad_widget() {
        register_widget('ZB_Side_Ad_Widget');
    }
    add_action('widgets_init', 'zb_register_side_ad_widget');
}

 

 

    请登录后查看评论内容