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

EDD特定分类单品页显示/隐藏Elementor容器的实现问题

解决EDD特定分类产品页添加Elementor容器的问题

方法一:调用已保存的Elementor模板(推荐)

这是最便捷的实现方式,先在Elementor里做好你需要的容器/部件并保存为模板,再通过代码调用:

  1. 在Elementor编辑器中创建目标容器内容,保存为「模板」,记下模板ID(可在模板列表查看,或编辑模板时从URL提取)。
  2. 修改现有代码,添加模板渲染逻辑,同时增加EDD产品页面的判断:
function display_condition_elementor_container() {
    global $post;

    // 仅在EDD产品单品页且属于Books分类时执行
    if ( is_singular( 'download' ) && has_term( 'Books', 'download_category' ) ) {
        // 替换为你的Elementor模板ID
        $template_id = 123;
        // 渲染指定模板
        echo Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $template_id );
    }
}
// 挂到EDD产品内容之后的钩子,也可根据需求换成edd_before_download_content(内容前显示)
add_action( 'edd_after_download_content', 'display_condition_elementor_container' );

方法二:直接渲染Elementor部件(无需保存模板)

如果不想依赖预先保存的模板,可以直接在代码中构建Elementor部件并渲染,以添加带文本的容器为例:

function display_condition_elementor_container() {
    global $post;

    if ( is_singular( 'download' ) && has_term( 'Books', 'download_category' ) ) {
        // 获取Elementor部件管理器实例
        $widget_manager = \Elementor\Plugin::instance()->widgets_manager;

        // 配置Container部件属性
        $container_settings = [
            'layout' => 'flex',
            'align_items' => 'center',
            'background_color' => '#f5f5f5',
            'padding' => '20px',
            'border_radius' => '8px',
        ];

        // 配置Text编辑器部件内容
        $text_settings = [
            'editor' => '<p>这是仅在Books分类产品页显示的专属内容</p>',
            'text_align' => 'center',
        ];

        // 构建Elementor内容结构
        $element_data = [
            'elType' => 'container',
            'settings' => $container_settings,
            'elements' => [
                [
                    'elType' => 'widget',
                    'widgetType' => 'text-editor',
                    'settings' => $text_settings,
                ],
            ],
        ];

        // 渲染构建好的Elementor内容
        $frontend = \Elementor\Plugin::instance()->frontend;
        $frontend->add_render_attribute( 'container', 'class', 'custom-edd-book-container' );
        echo $frontend->get_builder_content( $element_data, true );
    }
}
add_action( 'edd_after_download_content', 'display_condition_elementor_container' );

注意事项

  • 确保主题兼容Elementor渲染逻辑,避免样式丢失。
  • 钩子可按需调整:除了EDD专属钩子,也可用WordPress通用钩子the_content(注意设置合适的优先级)。
  • 代码生效前请清除网站缓存,避免旧内容干扰测试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 09:42:33