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

如何在TYPO3 v10实现工作日9-17点定时展示内容元素?

在TYPO3 v10中按工作日时段控制内容元素展示的可行方案

针对你需要仅在周一至周五9:00-17:00展示指定内容元素的需求,以下是几种可行的实现方案:


方案1:TypoScript条件控制(快速前端实现)

TYPO3的TypoScript支持基于当前时间的条件判断,无需额外扩展即可快速实现内容的前端显示控制。

针对单个内容元素(按UID)

如果目标是特定UID的内容元素,可使用如下配置:

# 仅在工作日9-17点显示UID为123的内容元素
[date("N") < 6 && date("H") >= 9 && date("H") < 17]
    # 假设该内容原本属于page.20的渲染区域
    page.20.render.10 = RECORDS
    page.20.render.10 {
        tables = tt_content
        source = 123
        dontCheckPid = 1
    }
[else]
    # 不满足条件时清空该内容渲染
    page.20.render.10 >
[end]

针对特定列的内容(按colPos)

如果要控制整个列的内容展示,可直接过滤对应colPos的内容:

# 控制colPos=2的内容仅在工作日9-17点显示
[date("N") < 6 && date("H") >=9 && date("H") <17]
    page.20 < styles.content.get
    page.20.select.where = colPos=2
[else]
    page.20 >
[end]

说明:date("N")返回1-7(周一到周日),<6代表周一至周五;date("H")为24小时制小时数,确保时间在9:00到17:00之间。
注意:此方案是前端层面隐藏内容,内容仍会出现在页面源码中,若需要完全屏蔽(如避免搜索引擎抓取),建议使用方案2。


方案2:自定义调度器任务(数据库层面控制)

若需要真正模拟"发布/取消发布"(修改tt_content表的starttime和endtime字段),可编写自定义调度器任务,定时更新内容的可见性。

步骤1:创建调度器任务类

在你的自定义扩展中创建任务逻辑:

<?php
namespace Vendor\YourExt\Scheduler;

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Scheduler\Task\AbstractTask;

class WorkdayContentTask extends AbstractTask {
    // 允许在调度器设置中输入要控制的内容UID(逗号分隔)
    public $contentUids = '';

    public function execute() {
        $contentUids = GeneralUtility::intExplode(',', $this->contentUids, true);
        if (empty($contentUids)) return false;

        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tt_content');
        $currentTime = time();
        $dayOfWeek = date('N', $currentTime);

        // 9点执行:设置当天17点为过期时间
        if (date('H', $currentTime) === '09' && $dayOfWeek < 6) {
            $endTime = strtotime('today 17:00');
            $connection->update(
                'tt_content',
                ['starttime' => $currentTime, 'endtime' => $endTime],
                ['uid' => $contentUids]
            );
        }
        // 17点执行:将过期时间设为过去,隐藏内容
        elseif (date('H', $currentTime) === '17') {
            $connection->update(
                'tt_content',
                ['endtime' => strtotime('-1 day')],
                ['uid' => $contentUids]
            );
        }
        return true;
    }
}

步骤2:注册调度器任务

在ext_localconf.php中添加任务注册代码:

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\Vendor\YourExt\Scheduler\WorkdayContentTask::class] = [
    'extension' => 'yourext',
    'title' => '工作日内容定时开关',
    'description' => '在工作日9点发布、17点隐藏指定内容',
    'additionalFields' => \Vendor\YourExt\Scheduler\WorkdayContentTaskFields::class
];

步骤3:配置调度器

在TYPO3后台的「系统 > 调度器」中添加两个任务:

  1. 任务1:执行时间设为0 9 * * 1-5(每周一至周五9点),输入要控制的内容UID
  2. 任务2:执行时间设为0 17 * * *(每天17点),输入相同的内容UID

方案3:使用替代扩展(无代码方案)

若不想编写代码,可尝试content_visibility扩展(确认兼容TYPO3 v10),它支持更灵活的时间规则配置,包括每周重复的工作日时段,可替代无法满足需求的Content Scheduler。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 06:55:21