返回列表 发布新帖
查看: 49|回复: 0

[代码技巧] 子比主题 – 文章页添加顶部信息美化

[复制链接]
SunJu_FaceMall
社区贡献

315

主题

190

回帖

1万

积分

等级头衔
Icon组别 : 管理员
Icon等级 :

积分成就
   钻石 : 524 颗
   贡献 : 1457 点
   金币 : 12 枚
Icon在线时间 : 1294 小时
Icon注册时间 : 2024-11-22
Icon最后登录 : 2025-10-29

荣誉勋章

会员LV.1会员LV.2会员LV.3会员LV.4会员LV.5会员LV.6会员LV.7会员LV.8会员LV.9会员LV.10

风云·优秀版主

飞流名人堂成员

1

实名认证 手机认证 vip vip-year FLLTCN发表于 2025-10-8 22:11:34 | 查看全部 |阅读模式 浙江金华

资源无需等待,交易就趁现在,全面资源整合网络大咖云集,让你轻松玩转互联网!

您需要 登录 才可以下载或查看,没有账号?立即注册

×
给子比主题文章页面的顶部加一个美化样式,看上去比较的二次元,喜欢的自行部署吧!

tengfei_down (38).webp

代码部署


将下面的代码放到:/wp-content/themes/zibll/func.php文件,没有这个文件自己创一个,记得加上php头,要不然会报错,将代码放里面即可!
  1. // 定义一个函数来输出文章头部相关内容
  2. function custom_output_article_header() {
  3.     ob_start();
  4.     global $post;
  5.     if ( is_single() && $post ) {
  6.         $categories = get_the_category( $post->ID );
  7.         $category_links = array();
  8.         if ( $categories ) {
  9.             foreach ( $categories as $category ) {
  10.                 $category_link = sprintf(
  11.                     '<a href="%s" style="background: var(--article-button-bg, #ff7eb9); color: var(--article-button-text, #fff); padding: 4px 8px; border-radius: 12px; text-decoration: none; font-weight: bold;">🏷️ %s</a>',
  12.                     esc_url( get_category_link( $category->term_id ) ),
  13.                     esc_html( $category->name )
  14.                 );
  15.                 $category_links[] = $category_link;
  16.             }
  17.         }
  18. ?>
  19.     <div class="article-header" style="background: var(--article-bg, linear-gradient(135deg, #ffd1dc, #c1e1ff)); padding: 15px; border-radius: 16px; border: 3px solid var(--article-border, transparent); margin: 20px 0; font-size: 14px; color: var(--article-text, #333); position: relative; overflow: hidden; box-shadow: 0 4px 12px var(--article-shadow, rgba(255, 126, 185, 0.3)); transition: transform 0.3s ease, box-shadow 0.3s ease; width: 100%; max-width: 100%; text-align: center;">
  20.         <!-- 二次元装饰图标 -->
  21.         <span style="position: absolute; top: -20px; right: -20px; font-size: 60px; color: var(--article-icon, #ff7eb9); opacity: 0.2; transform: rotate(30deg);">🎀</span>
  22.         <span style="position: absolute; bottom: -20px; left: -20px; font-size: 60px; color: var(--article-icon, #ff7eb9); opacity: 0.2; transform: rotate(-30deg);">🌸</span>

  23.         <!-- 动态悬浮效果 -->
  24.         <div class="particle-effect" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: radial-gradient(circle, var(--article-particle, rgba(255, 126, 185, 0.2)) 10%, transparent 10.01%); background-size: 20px 20px; animation: move 3s infinite linear;"></div>

  25.         <!-- 文章信息内容 -->
  26.         <div style="position: relative; z-index: 2;">
  27.             <h1 class="article-title" style="font-size: 20px; color: var(--article-title, #ff7eb9); margin-bottom: 10px; font-family: 'Comic Sans MS', cursive, sans-serif;">
  28.                 <?php the_title();?>
  29.             </h1>
  30.             <!-- 作者、分类、标签 -->
  31.             <div class="article-meta-buttons" style="display: flex; justify-content: center; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px;">
  32.                 <!-- 作者 -->
  33.                 <span style="display: flex; align-items: center; gap: 4px;">
  34.                     <a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) );?>" style="background: var(--article-button-bg, #ff7eb9); color: var(--article-button-text, #fff); padding: 4px 8px; border-radius: 12px; text-decoration: none; font-weight: bold;">📝 <?php the_author();?></a>
  35.                 </span>
  36.                 <!-- 分类 -->
  37.                 <?php if ( $category_links ) {
  38.                     foreach ( $category_links as $category_link ) {
  39.                         echo $category_link;
  40.                     }
  41.                 }?>
  42.                 <!-- 标签 -->
  43.             </div>
  44.                     </div>
  45.     </div>
  46.     <style>
  47.         @keyframes move {
  48.             0% {
  49.                 background-position: 0 0;
  50.             }
  51.             100% {
  52.                 background-position: 20px 20px;
  53.             }
  54.         }

  55.         /* 悬浮效果 */
  56.        .article-header:hover.particle-effect {
  57.             background: radial-gradient(circle, var(--article-particle, rgba(255, 126, 185, 0.4)) 10%, transparent 10.01%);
  58.             animation: move 0.5s linear;
  59.         }

  60.         /* 点击特效 */
  61.        .article-header:active.particle-effect {
  62.             background: radial-gradient(circle, var(--article-particle, rgba(255, 126, 185, 0.6)) 10%, transparent 10.01%);
  63.             animation: move 0.2s linear;
  64.         }

  65.         /* 日间模式样式 */
  66.         :root {
  67.             --article-bg: linear-gradient(135deg, #ffd1dc, #c1e1ff);
  68.             --article-border: transparent;
  69.             --article-text: #333;
  70.             --article-title: #ff7eb9;
  71.             --article-icon: #ff7eb9;
  72.             --article-shadow: rgba(255, 126, 185, 0.3);
  73.             --article-shadow-hover: rgba(255, 126, 185, 0.4);
  74.             --article-particle: rgba(255, 126, 185, 0.2);
  75.             --article-button-bg: #ff7eb9;
  76.             --article-button-text: #fff;
  77.         }
  78. /* 夜间模式样式 */
  79.         [data-theme='dark'] {
  80.             --article-bg: linear-gradient(135deg, #3a1f4a, #1a1a2e);
  81.             --article-border: transparent;
  82.             --article-text: #ddd;
  83.             --article-title: #ff7eb9;
  84.             --article-icon: #ff7eb9;
  85.             --article-shadow: rgba(255, 126, 185, 0.2);
  86.             --article-shadow-hover: rgba(255, 126, 185, 0.3);
  87.             --article-particle: rgba(255, 126, 185, 0.1);
  88.             --article-button-bg: #ff7eb9;
  89.             --article-button-text: #fff;
  90.         }

  91.         /* 手机端优化 */
  92.         @media (max-width: 768px) {
  93.            .article-header {
  94.                 padding: 12px;
  95.                 border-radius: 12px;
  96.             }
  97.            .article-title {
  98.                 font-size: 18px!important;
  99.             }
  100.            .article-meta-buttons,.article-meta-info {
  101.                 gap: 8px;
  102.             }
  103.            .article-meta-buttons a,.article-meta-info span {
  104.                 font-size: 12px;
  105.                 padding: 4px 8px;
  106.             }
  107.         }
  108.     </style>
  109. <?php
  110.     }
  111.     $html_content = ob_get_clean();
  112.     echo $html_content;
  113. }
  114. add_action('zib_single_before', 'custom_output_article_header');
  115. // 这里是一个简单的估算阅读时长的函数示例,你可以根据实际需求调整
  116. function estimate_reading_time() {
  117.     global $post;
  118.     $word_count = str_word_count( strip_tags( $post->post_content ) );
  119.     $reading_time = ceil( $word_count / 200 ); // 假设每分钟阅读200字
  120.     return $reading_time. " 分钟";
  121. }
复制代码

本帖被以下淘专辑推荐:

路虽远,行则将至;事虽难,做则必成。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

飞流广播+ 发布

系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
10-30 17:02
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
10-09 09:07
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-24 09:00
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-11 11:40
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
09-02 09:17
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-27 08:56
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-20 15:12
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
08-03 10:22
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
06-30 08:57
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
06-18 09:14
系统消息:清风网络已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
04-11 09:40
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-10 09:31
系统消息:IXM77777已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-09 13:44
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-09 09:22
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
04-09 08:52
系统消息:清风网络已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-08 09:24
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
04-07 09:02
系统消息:柒沐已经连续答对10道难题,逆天学霸谁与争锋?!#每日答题#
02-27 09:35
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
02-26 09:06
系统消息:柒沐已经连续答对3道难题,逆天学霸谁与争锋?!#每日答题#
02-25 08:49
站内通告

提供资源交易、信息共享、靓号交流、技术变现、学习问答、兴趣娱乐等全面服务。

1.丰富功能系统,扩展社区特色玩法,打造最好的互联网聚集圈子。

2.准确信息真实交易,安全快捷又方便,让虚拟交易面对面。

3. 天上不会掉馅饼,话术骗术迷人心,切勿脱离平台线下交易,被骗与平台无关!

4. 欺诈骗钱,违规违法将视情受到警告&禁言&封号甚至检举至👮🏻‍♀️处理!

官方Q群:123129钉推群:BAYR2383 站长QQ:3220000000

投诉/建议/商务合作联系

fl@fllt.cn

严禁私下交易,被骗与本站无关。
违反交易细则,取证立查严惩。
  • 钉钉新帖推送群
  • 官方交流QQ群
  • 站长唯一微信号

👮曝光Ta|🧿小黑屋|📴手机页|飞流网 ( 渝ICP备2025054677号-1|电信增值许可 渝B2-20250789 )|网站地图

GMT+8, 2025-11-8 11:32 , Processed in 0.086335 second(s), 61 queries, MemCached On , Gzip On.

Based on XJ-TX X3.5 Licensed

飞流论坛 HanAnalytics icp Astro vhAstro-Theme

关灯 在本版发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表