To display/hide a article by AD control,please refer below advice.模块安装成功了,但现在的逻辑是:模块只显示广告,而不控制文章的显示/隐藏。要实现"不点击广告就不显示文章",我们需要修改文章本身的显示逻辑。
方案1:修改文章显示方式(推荐) 修改模块,让它控制文章的显示。修改你的 mod_adunlock.php 文件,添加控制文章显示的逻辑:
get('content_id'); $adId = (int) $params->get('ad_id'); $adCode = $params->get('ad_code', ''); $unlockDuration = (int) $params->get('unlock_duration', 24); $lockedMessage = $params->get('locked_message', '
Content locked.
'); // Get current user and check unlock status $user = Factory::getUser(); $app = Factory::getApplication(); $input = $app->input; $currentArticleId = $input->getInt('id', 0); // 检查是否是目标文章 $isTargetArticle = ($contentId > 0 && $currentArticleId == $contentId); // 只有在目标文章页面才执行解锁检查 if ($isTargetArticle) { $unlocked = ModAdunlockHelper::isContentUnlocked($contentId, $user->id, $unlockDuration); // ========== 重要:如果未解锁,隐藏文章内容 ========== if (!$unlocked) { // 方法1:通过CSS隐藏文章内容区域 $doc = Factory::getDocument(); $doc->addStyleDeclaration(' /* 隐藏文章内容 */ .item-page .article-body, .item-page .article-content, .item-page .article-text, .item-page [itemprop="articleBody"], .item-page .content-article, #article-content, .article-fulltext, .article-intro, .article-body > * { display: none !important; } /* 显示解锁提示 */ .adunlock-article-placeholder { display: block !important; background: #fff3cd; border: 2px solid #ffc107; border-radius: 8px; padding: 30px; margin: 30px 0; text-align: center; box-shadow: 0 4px 12px rgba(0,0,0,0.1); } '); // 方法2:输出JavaScript来动态隐藏/替换文章内容 $doc->addScriptDeclaration(' document.addEventListener("DOMContentLoaded", function() { // 隐藏文章内容 var articleSelectors = [ ".item-page .article-body", ".item-page .article-content", ".item-page .article-text", ".item-page [itemprop=\'articleBody\']", ".content-article", "#article-content", ".article-fulltext" ]; articleSelectors.forEach(function(selector) { var elements = document.querySelectorAll(selector); elements.forEach(function(el) { el.style.display = "none"; }); }); // 在文章位置添加解锁提示 var articleContent = document.querySelector(".item-page .article-body") || document.querySelector(".item-page .article-content") || document.querySelector("#article-content"); if (articleContent && articleContent.parentNode) { var placeholder = document.createElement("div"); placeholder.className = "adunlock-article-placeholder"; placeholder.innerHTML = \'🔒 Premium Content Locked
This article requires viewing an advertisement to unlock.
The advertisement is shown below. Please click it to view the article.
\'; articleContent.parentNode.insertBefore(placeholder, articleContent); } }); '); } } // 加载模块CSS/JS $doc = Factory::getDocument(); $wa = $doc->getWebAssetManager(); $wa->registerAndUseStyle('mod_adunlock.css', 'mod_adunlock/adunlock.css'); $wa->registerAndUseScript('mod_adunlock.js', 'mod_adunlock/adunlock.js', [], ['defer' => true]); // Pass data to JavaScript $doc->addScriptOptions('mod_adunlock', [ 'contentId' => $contentId, 'adId' => $adId, 'ajaxUrl' => 'index.php?option=com_ajax&module=adunlock&format=json', 'token' => Factory::getSession()->getToken(), 'unlockDuration' => $unlockDuration ]); // 只在目标文章页面显示模块 if ($isTargetArticle) { if (isset($unlocked) && $unlocked) { // 已经解锁:显示解锁消息 require ModuleHelper::getLayoutPath('mod_adunlock', 'unlocked'); // 添加解锁后的样式 - 显示文章内容 $doc->addScriptDeclaration(' document.addEventListener("DOMContentLoaded", function() { // 显示文章内容 var articleSelectors = [ ".item-page .article-body", ".item-page .article-content", ".item-page .article-text", ".item-page [itemprop=\'articleBody\']", ".content-article", "#article-content", ".article-fulltext" ]; articleSelectors.forEach(function(selector) { var elements = document.querySelectorAll(selector); elements.forEach(function(el) { el.style.display = ""; }); }); // 移除解锁提示 var placeholders = document.querySelectorAll(".adunlock-article-placeholder"); placeholders.forEach(function(el) { el.parentNode.removeChild(el); }); }); '); } else { // 未解锁:显示广告 $displayData = [ 'params' => $params, 'lockedMessage' => $lockedMessage, 'adCode' => $adCode, 'unlockDuration' => $unlockDuration, 'module' => $module, 'moduleclass_sfx' => htmlspecialchars($params->get('moduleclass_sfx', ''), ENT_COMPAT, 'UTF-8') ]; require ModuleHelper::getLayoutPath('mod_adunlock', 'default'); } } else { // 如果不是目标文章,显示提示信息 echo '
方案2:使用模板覆盖(更可靠)
创建模板覆盖文件来修改文章显示:
- 在你的 Joomla 模板中创建覆盖文件:
- 复制 /components/com_content/tmpl/article/default.php
- 到 /templates/YOUR_TEMPLATE/html/com_content/article/default.php
- 修改这个文件,添加解锁检查:
YOUR_TEMPLATE_html_com_content_article_default.php
item->id; $user = Factory::getUser(); $app = Factory::getApplication(); // 检查是否有Ad Unlock模块 $db = Factory::getDbo(); $query = $db->getQuery(true) ->select('params') ->from('#__modules') ->where('module = ' . $db->quote('mod_adunlock')) ->where('published = 1'); $db->setQuery($query); $modules = $db->loadObjectList(); $isLocked = false; $moduleParams = null; foreach ($modules as $module) { $params = json_decode($module->params); if ($params->content_id == $articleId) { $isLocked = true; $moduleParams = $params; break; } } // 如果文章被锁定,检查解锁状态 if ($isLocked) { // 加载模块的helper类 $helperPath = JPATH_ROOT . '/modules/mod_adunlock/helper.php'; if (file_exists($helperPath)) { require_once $helperPath; $unlockDuration = isset($moduleParams->unlock_duration) ? (int)$moduleParams->unlock_duration : 24; $unlocked = ModAdunlockHelper::isContentUnlocked($articleId, $user->id, $unlockDuration); if (!$unlocked) { // 隐藏文章内容,显示广告区域 echo '
Premium Content Locked
'; echo 'This article requires viewing an advertisement to unlock. The advertisement will be displayed below.
'; echo '
方案3:简单修改(快速测试)
在你的模板CSS中添加:
/* 在模块未解锁时隐藏文章内容 */.mod-adunlock:not(.unlocked) ~ .item-page .article-body,.mod-adunlock:not(.unlocked) ~ .item-page .article-content,.mod-adunlock:not(.unlocked) ~ .item-page [itemprop="articleBody"] { display: none !important;}
/* 添加解锁提示 */.mod-adunlock:not(.unlocked) ~ .item-page::before { content: "🔒 Premium Content Locked - Please view the advertisement below to unlock this article"; display: block; background: #fff3cd; border: 2px solid #ffc107; border-radius: 8px; padding: 20px; margin: 20px 0; text-align: center; font-weight: bold;}
更新你的构建脚本
修改 PowerShell 脚本中的 mod_adunlock.php 部分,使用方案1的代码。重新生成模块并安装。
测试步骤:
- 安装新模块后,访问目标文章
- 文章内容应该被隐藏,只显示广告模块
- 点击广告后,页面刷新
- 文章内容应该显示,并显示"已解锁"消息
重要提示:
- 模块位置:确保模块放在文章内容之前,这样CSS/JavaScript才能正确工作
- Content ID:一定要正确设置文章ID
- 模板兼容性:不同模板的文章内容CSS选择器可能不同,你可能需要根据你的模板调整选择器
如果你需要更精确的控制:
创建一个简单的插件来拦截文章内容:
plugins_content_adunloc_adunlock.php