You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求Woodmart子主题PHP代码:导入任意演示模板后自动替换默认Logo

替换Woodmart演示模板导入后的默认Logo(子主题兼容)

要实现导入任意Woodmart演示模板后自动替换默认Logo,你可以在子主题的functions.php中添加以下代码,它会监听演示导入完成的钩子,批量更新所有相关的Logo设置项,确保兼容所有70+演示模板:

/**
 * 替换Woodmart演示导入后的默认Logo
 */
function custom_woodmart_replace_logo_after_demo_import() {
    // 替换为你上传到媒体库的自定义Logo ID
    $custom_logo_id = 123; 
    // 可选:如果需要替换深色模式Logo,设置对应的ID
    $custom_dark_logo_id = 124; 
    // 可选:如果需要替换移动端Logo,设置对应的ID
    $custom_mobile_logo_id = 125; 
    // 可选:如果需要替换移动端深色Logo,设置对应的ID
    $custom_mobile_dark_logo_id = 126; 

    // 更新WordPress默认Logo设置
    update_option('custom_logo', $custom_logo_id);

    // 更新Woodmart主题专属的Logo设置项
    $woodmart_logo_options = [
        'logo' => $custom_logo_id,
        'logo_dark' => $custom_dark_logo_id,
        'logo_mobile' => $custom_mobile_logo_id,
        'logo_mobile_dark' => $custom_mobile_dark_logo_id,
        'logo_sticky' => $custom_logo_id,
        'logo_sticky_dark' => $custom_dark_logo_id,
    ];

    foreach ($woodmart_logo_options as $option_key => $option_value) {
        if ($option_value) {
            update_option('woodmart_' . $option_key, $option_value);
        }
    }
}
add_action('woodmart_after_demo_import', 'custom_woodmart_replace_logo_after_demo_import');

使用说明:

  1. 先将你的自定义Logo上传到WordPress媒体库,记下每个Logo的ID(可以在媒体库编辑页面的URL中找到,比如post=123中的123就是ID)。
  2. 将代码中的$custom_logo_id及其他可选变量替换为你自己的Logo ID,如果不需要某类Logo(比如深色模式),可以直接留空或删除对应行。
  3. 将代码添加到你的Woodmart子主题的functions.php文件中,不要直接修改父主题文件,避免主题更新丢失修改。

代码说明:

  • 使用woodmart_after_demo_import钩子:这是Woodmart主题提供的演示导入完成后的专属钩子,确保代码只在演示导入完成后执行。
  • 覆盖所有相关Logo设置:同时更新WordPress默认Logo和Woodmart主题特有的Logo选项(包括深色、移动端、粘性导航Logo等),确保所有场景下的Logo都被替换。

内容的提问来源于stack exchange,提问作者Nabeel Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 02:25:44