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

如何为特定Vendor ID的商店列表在商家名称后添加自定义徽章

如何为特定Vendor ID的商家在商店列表名称后添加自定义徽章?

现有代码可实现徽章展示,但会对所有商家生效,需要修改代码使其仅针对特定Vendor ID的商家生效。

效果示意:商家名称后添加自定义徽章的效果

原代码

function display_author_name_store_listing() {
    if (!is_plugin_active('dokan-lite/dokan.php')){
        return"";
    }
    printf("custom badge");
}   
add_action( 'dokan_store_list_loop_after_store_name', 'display_author_name_store_listing');

修改后的代码

function display_author_name_store_listing() {
    // 检查Dokan插件是否激活
    if (!is_plugin_active('dokan-lite/dokan.php')){
        return;
    }

    // 获取当前商家的Vendor ID
    global $store;
    $vendor_id = $store->id;

    // 定义需要添加徽章的目标Vendor ID列表,把123、456换成你实际需要的商家ID
    $target_vendor_ids = array(123, 456);

    // 仅对目标商家输出徽章
    if (in_array($vendor_id, $target_vendor_ids)) {
        printf("<span class='custom-vendor-badge'>专属徽章</span>");
    }
}   
add_action( 'dokan_store_list_loop_after_store_name', 'display_author_name_store_listing');

修改说明

  • 引入全局变量$store,直接拿到当前循环里商家的ID
  • 数组$target_vendor_ids用来指定要显示徽章的商家ID,多个ID用逗号隔开就行
  • 用in_array()判断当前商家是否在目标列表里,只有符合条件才会输出徽章
  • 可以给徽章加custom-vendor-badge类,之后用CSS就能调整徽章的样式,比如背景色、字体大小这些

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 17:17:31