在子比主题卡片右上角添加发布时间的代码

文章最后更新时间:2025-04-19 23:02:47

方法一:通过CSS定位添加

打开子比主题的样式文件(通常是style.css)或通过WordPress自定义CSS选项

添加下CSS代码:

/* 卡片右上角添加发布时间 */
.zib_post .list-inline-item.post-time {
    position: absolute;
    top: 10px;
    right: 10px;
    background-color: rgba(0,0,0,0.5);
    color: #fff;
    padding: 2px 8px;
    border-radius: 3px;
    font-size: 12px;
}

然后修改对应的模板文件(通常是content.php或类似文件),找到卡片部分,添加时间显示代码:

<span class="list-inline-item post-time"><?php echo get_the_date(); ?></span>

方法二:通过子比主题的钩子函数

如果你熟悉WordPress钩子,可以使用子比主题提供的钩子来添加:

<div class="post-meta">
    <?php if(!empty($time)): ?>
    <span class="post-time"><?php echo get_the_date(); ?></span>
    <?php endif; ?>
    <!-- 其他元信息 -->
</div>

方法三:修改模板文件

找到子比主题的卡片模板文件(通常在 template-parts 目录下)

在卡片头部区域添加时间

<div class="post-meta">
    <?php if(!empty($time)): ?>
    <span class="post-time"><?php echo get_the_date(); ?></span>
    <?php endif; ?>
    <!-- 其他元信息 -->
</div>

方法二:使用子比主题的钩子添加(推荐) 如果你不想直接修改模板文件,可以使用子比主题的钩子:

// 添加到子主题的functions.php中
function zib_add_post_time_to_card($content) {
    if (is_main_query() && in_the_loop()) {
        $time = '<div class="post-time"><i class="fa fa-clock-o"></i> ' . get_the_date() . '</div>';
        return $content . $time;
    }
    return $content;
}
add_filter('the_content', 'zib_add_post_time_to_card');

/* 对应的CSS */
.post-time {
margin-top: 10px;
font-size: 12px;
color: #999;
text-align: right;
padding: 5px 10px;
background: #f7f7f7;
border-radius: 3px;
display: inline-block;
}

.post-time i {
margin-right: 5px;
}

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容