Back to Main Site

挂钩:post.default_image

Last updated on Jun 24, 2026 02:07

## 概述

类型: 过滤器 位置: app/helpers.php - get_default_post_image()

允许主题和模块覆盖帖子没有特色图像时显示的后备图像。管理员通过 设置 > 阅读 > 默认帖子图像 设置全局默认值。该过滤器在检索到该值后运行,使开发人员有机会根据上下文返回不同的 URL。

## 参数

参数 类型 描述
$imageUrl string or null 设置中的当前默认图像 URL
$context mixed 发布模型实例(或 null)

## 返回

必须返回 string|null - 要使用的图像 URL,或返回 null 以回退到 SVG 占位符。

使用示例

为每个帖子类别设置不同的默认图像:

use App\Facades\Hook;

Hook::addFilter('post.default_image', function (?string $url, $post) {
    if ($post?->categories->contains('slug', 'technology')) {
        return '/images/defaults/tech-cover.jpg';
    }
    return $url;
});

相关挂钩