广告内容存储在数据库


如果您希望将广告内容存储在数据库中,并自己建立表和列,那么可以采用以下方法:

  1. 创建自定义数据表
    在数据库中创建一个自定义的数据表,用于存储手机版和PC版的广告内容。例如:
CREATE TABLE `wp_ads` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `mobile_ad` text DEFAULT NULL,
  `pc_ad` text DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  1. 在主题的 functions.php 文件中,添加以下代码来创建管理页面:
// 创建自定义页面
add_action('admin_menu', 'add_ads_menu');
function add_ads_menu() {
    add_menu_page(
        'Ads Management',
        'Ads',
        'manage_options',
        'ads-management',
        'ads_management_page',
        'dashicons-admin-settings',
        6
    );
}

function ads_management_page() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'ads';

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $mobile_ad = $_POST['mobile_ad'];
        $pc_ad = $_POST['pc_ad'];
        $wpdb->insert($table_name, array(
            'mobile_ad' => $mobile_ad,
            'pc_ad' => $pc_ad
        ));
    }

    $ads = $wpdb->get_results("SELECT * FROM $table_name ORDER BY id DESC");
    ?>
    <div class="wrap">
        <h1>Ads Management</h1>
        <form method="post" action="">
            <table class="form-table">
                <tr>
                    <th scope="row">Mobile Ad</th>
                    <td><textarea name="mobile_ad" rows="5" class="large-text"></textarea></td>
                </tr>
                <tr>
                    <th scope="row">PC Ad</th>
                    <td><textarea name="pc_ad" rows="5" class="large-text"></textarea></td>
                </tr>
            </table>
            <?php submit_button(); ?>
        </form>
        <h2>Ads History</h2>
        <table class="wp-list-table widefat striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Mobile Ad</th>
                    <th>PC Ad</th>
                    <th>Created At</th>
                    <th>Updated At</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($ads as $ad) { ?>
                    <tr>
                        <td><?php echo $ad->id; ?></td>
                        <td><?php echo $ad->mobile_ad; ?></td>
                        <td><?php echo $ad->pc_ad; ?></td>
                        <td><?php echo $ad->created_at; ?></td>
                        <td><?php echo $ad->updated_at; ?></td>
                    </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
    <?php
}
  1. 在首页模板文件(front-page.phphome.php)中,添加以下代码来显示广告内容:
<?php
/*
Template Name: Home Page
*/

get_header(); ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main">
        <div class="row">
            <div class="col-md-6">
                <?php
                global $wpdb;
                $table_name = $wpdb->prefix . 'ads';
                $latest_ad = $wpdb->get_row("SELECT mobile_ad FROM $table_name ORDER BY id DESC LIMIT 1");
                if ($latest_ad && $latest_ad->mobile_ad) {
                    echo $latest_ad->mobile_ad;
                } else {
                    echo 'No mobile ad content found.';
                }
                ?>
            </div>
            <div class="col-md-6">
                <?php
                $latest_ad = $wpdb->get_row("SELECT pc_ad FROM $table_name ORDER BY id DESC LIMIT 1");
                if ($latest_ad && $latest_ad->pc_ad) {
                    echo $latest_ad->pc_ad;
                } else {
                    echo 'No PC ad content found.';
                }
                ?>
            </div>
        </div>
    </main><!-- #main -->
</div><!-- #primary -->

<?php
get_sidebar();
get_footer();
?>

这样,您就可以在 WordPress 后台的”Ads”菜单下管理广告内容,并在首页模板中显示最新添加的广告。

如果您有任何其他问题,欢迎随时告诉我。

好的,我理解您现在有多个广告位置需要管理。我可以帮您扩展之前的代码,增加两组新的广告位置。

  1. 首先,我们需要在数据库中添加两个新的列来存储这两个新的广告位置:
ALTER TABLE `wp_ads`
  ADD `banner_ad` text DEFAULT NULL,
  ADD `sidebar_ad` text DEFAULT NULL;
  1. 然后,在管理页面的表单中添加这两个新的字段:
<table class="form-table">
    <tr>
        <th scope="row">Mobile Ad</th>
        <td><textarea name="mobile_ad" rows="5" class="large-text"></textarea></td>
    </tr>
    <tr>
        <th scope="row">PC Ad</th>
        <td><textarea name="pc_ad" rows="5" class="large-text"></textarea></td>
    </tr>
    <tr>
        <th scope="row">Banner Ad</th>
        <td><textarea name="banner_ad" rows="5" class="large-text"></textarea></td>
    </tr>
    <tr>
        <th scope="row">Sidebar Ad</th>
        <td><textarea name="sidebar_ad" rows="5" class="large-text"></textarea></td>
    </tr>
</table>
  1. 更新插入数据的代码,包括新的字段:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $mobile_ad = $_POST['mobile_ad'];
    $pc_ad = $_POST['pc_ad'];
    $banner_ad = $_POST['banner_ad'];
    $sidebar_ad = $_POST['sidebar_ad'];
    $wpdb->insert($table_name, array(
        'mobile_ad' => $mobile_ad,
        'pc_ad' => $pc_ad,
        'banner_ad' => $banner_ad,
        'sidebar_ad' => $sidebar_ad
    ));
}
  1. 在历史记录表格中添加新的列:
<thead>
    <tr>
        <th>ID</th>
        <th>Mobile Ad</th>
        <th>PC Ad</th>
        <th>Banner Ad</th>
        <th>Sidebar Ad</th>
        <th>Created At</th>
        <th>Updated At</th>
    </tr>
</thead>
<tbody>
    <?php foreach ($ads as $ad) { ?>
        <tr>
            <td><?php echo $ad->id; ?></td>
            <td><?php echo $ad->mobile_ad; ?></td>
            <td><?php echo $ad->pc_ad; ?></td>
            <td><?php echo $ad->banner_ad; ?></td>
            <td><?php echo $ad->sidebar_ad; ?></td>
            <td><?php echo $ad->created_at; ?></td>
            <td><?php echo $ad->updated_at; ?></td>
        </tr>
    <?php } ?>
</tbody>
  1. 最后,在首页模板中添加新的广告位置代码:
<div class="row">
    <div class="col-md-6">
        <?php
        $latest_ad = $wpdb->get_row("SELECT mobile_ad FROM $table_name ORDER BY id DESC LIMIT 1");
        if ($latest_ad && $latest_ad->mobile_ad) {
            echo $latest_ad->mobile_ad;
        } else {
            echo 'No mobile ad content found.';
        }
        ?>
    </div>
    <div class="col-md-6">
        <?php
        $latest_ad = $wpdb->get_row("SELECT pc_ad FROM $table_name ORDER BY id DESC LIMIT 1");
        if ($latest_ad && $latest_ad->pc_ad) {
            echo $latest_ad->pc_ad;
        } else {
            echo 'No PC ad content found.';
        }
        ?>
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        <?php
        $latest_ad = $wpdb->get_row("SELECT banner_ad FROM $table_name ORDER BY id DESC LIMIT 1");
        if ($latest_ad && $latest_ad->banner_ad) {
            echo $latest_ad->banner_ad;
        } else {
            echo 'No banner ad content found.';
        }
        ?>
    </div>
</div>
<div class="row">
    <div class="col-md-4">
        <?php
        $latest_ad = $wpdb->get_row("SELECT sidebar_ad FROM $table_name ORDER BY id DESC LIMIT 1");
        if ($latest_ad && $latest_ad->sidebar_ad) {
            echo $latest_ad->sidebar_ad;
        } else {
            echo 'No sidebar ad content found.';
        }
        ?>
    </div>
</div>

现在您就可以在 WordPress 后台的”Ads”菜单下管理4个不同的广告位置了。如果您有任何其他问题,随时告诉我。


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注