原創技術
# handsome # 程式碼 # 程式碼生活

刪除handsome相簿文章的圖片描述

1,382 字3 分鐘52 瀏覽0 按讚

摘要

本文介紹了Handsome主題中相簿文章圖片描述的刪除方法。使用者在上傳圖片時發現圖片帶有描述資訊,影響瀏覽體驗。透過修改主題目錄下的TypePost.php檔案,替換相關程式碼,移除圖片的alt屬性,從而在相簿頁面中只顯示圖片而不顯示描述。該操作簡便有效,建議備份原檔案。修改後圖片顯示更加簡潔,解決了視覺問題。

起因是我想把自己的相簿上傳到handsome主題中的相簿頁面的時候除了點問題,我上傳的圖片都是有描述的,這導致我的眼睛很受不了

修改前

请输入图片描述

修改後

请输入图片描述

終於是拯救了我的眼睛啊

修改方法

開啟目錄/usr/themes/handsome/libs/content/

找到TypePost.php檔案,將下面的程式碼全部複製,進行替換,注意做好備份!!!

<?php
/**
 * 多种不同的文章内容,目前支持相册和普通文章两种,有计划支持视频类型
 */

class TypePost{

    /**
     * @param $content
     * @param $obj
     * @return string
     */
    public static function postImagePost($content, $obj)
    {
        if ($obj->hidden === true) {//输入密码访问
            return $content;
        } else {
            return TypePost::parseContentToImage($content, "album");
        }
    }

    /**
     * 解析文章内容为图片列表(相册)
     * @param $content
     * @param $type
     * @return string
     */
    public static function parseContentToImage($content, $type)
    {
        preg_match_all('/<img.*?src="(.*?)"(.*?)(alt="(.*?)")??(.*?)\/?>/', $content, $matches);

        if (is_array($matches) && count($matches[0]) > 0) {

            $html = "";
            if ($type === "photos") {//自适应拉伸的
                $html .= "<div class='album-photos'>";
            } else {//统一宽度排列
                $html .= "<div class='photos'>";
            }
            for ($i = 0; $i < count($matches[0]); $i++) {
                $info = trim($matches[5][$i]);
                preg_match('/alt="(.*?)"/', $info, $info);
                if (is_array($info) && count($info) >= 2) {
                    $info = @$info[1];
                } else {
                    $info = "";
                }
                if ($type == "photos") {
                    $html .= <<<EOF
<figure>
        {$matches[0][$i]}
</figure>
EOF;
                } else {
                    $html .= <<<EOF
<figure class="image-thumb" itemprop="associatedMedia" itemscope="" itemtype="http://schema.org/ImageObject">
          {$matches[0][$i]}
      </figure>
EOF;
                }
            }

            $html .= "</div>";

            return $html;
        } else {
            //解析失败,就不解析,交给前端进行解析,还原之前的短代码
            $type = ($type == "photos") ? ' type="photos"' : "";
            return "<div class='album_block'>\n\n[album" . $type . "]\n" . $content . "[/album] </div>";
        }
    }
}
留言載入中…
相關文章

相關文章