想要獲取自定義字段的附件類型里面的數據地址,只能以自定義形式。當然以下辦法是比較笨的也不是很完美,但基礎達到目的就行了。
有用就使用吧!

效(xiao)果是(shi) 獲取自定義字(zi)段的附件類型 實際路徑。
操作方法:
打開\extend\function.php
文(wen)件最(zui)底(di)下添(tian)加一下代碼(ma):
if (!function_exists('get_dopdf_value'))
{
/**
* 查(cha)詢當前(qian)文章的dopdf字段的值
*/
function get_dopdf_value($aid = 0)
{
// 定義查(cha)詢(xun)條件
$condition = ['a.aid' => $aid];
// 查詢當(dang)前文章的dopdf字段的值
$record = \think\Db::name('article_content')
->alias('a')
->where($condition)
->value('dopdf');
return $record;
}
}
代碼說明:
(1).
如(ru)果(guo)(guo)你是(shi)要獲取 文章模型(xing)(xing)里(li)面的自(zi)定義下(xia)載 上面紅色就是(shi)模型(xing)(xing)內容表(biao)。 如(ru)果(guo)(guo)是(shi)其他模型(xing)(xing)的 比如(ru)下(xia)載的 請參考以下(xia)模型(xing)(xing) 一次把上面的紅色替換即可:
文章(zhang)模(mo)型內容表:article_content
下載模(mo)型內容表:download_content
視頻模型(xing)內容表:media_content
產(chan)品(pin)模(mo)型內容表:product_content
圖集模型內容表:images_content
按(an)上(shang)面模型替換以(yi)上(shang)紅(hong)色即可
【由于(yu)自定義字段附件存儲(chu)的是模型內(nei)容表(biao)里面(mian)】
(2)、
自定義添加的字段說(shuo)明:
比如自定義字段添加為:dopdf
那么上面將粉色字 替換(huan)你(ni)自己添加的即可

這2個改了 就(jiu)可以,合并模板調用標簽就(jiu)是:
{$eyou.field.aid|get_dopdf_value}
控制頁的就完成了。
同樣你(ni)有(you)多(duo)個(ge)模型(xing) 再安裝上面方法添加一個(ge)即(ji)可 當然你(ni)要把get_dopdf_value 這(zhe)個(ge) 修改(gai)為(wei)其他自(zi)己隨意即(ji)可 標簽就是 {$eyou.field.aid|自(zi)定義}
以下是具體:
if (!function_exists('自定義函數(shu)'))
{
/**
* 查(cha)詢當前文章的(de)dopdf字段的(de)值
*/
function 自定義(yi)函數($aid = 0)
{
// 定義查詢(xun)條件(jian)
$condition = ['a.aid' => $aid];
// 查詢當前文章(zhang)的dopdf字段的值(zhi)
$record = \think\Db::name('模型(xing)內容(rong)表')
->alias('a')
->where($condition)
->value('自(zi)定義字段(duan)名(ming)稱');
return $record;
}
}
第二部在 前端(duan)模板(ban) 添加以下代碼(ma):
<a href="{$eyou.field.aid|get_dopdf_value}" id="myLink">{$eyou.field.aid|get_dopdf_value}</a>
【JS放最底下-都可以】
<script>
const link = document.getElementById('myLink');
const href = link.href;
const pdfPos = href.indexOf('.pdf');
if (pdfPos!== -1) {
link.href = href.substring(0, pdfPos + 4);
link.textContent = href.substring(0, pdfPos + 4);
}
</script>
或者:
<script>
const fileTypes = ['.pdf', '.rar', '.zip', '.docx', '.xlsx'];
const links = document.querySelectorAll('a');
links.forEach(link => {
const href = link.href.toLowerCase();
const matchedType = fileTypes.find(type => href.includes(type));
if (matchedType) {
const typePos = href.indexOf(matchedType);
const cleanHref = href.substring(0, typePos + matchedType.length);
link.href = cleanHref;
link.textContent = cleanHref;
}
});
</script>
JS說明 紅色(se)字為類(lei)型,如果你是其它比如rar 就改下 .rar 即(ji)可 以(yi)上(shang)不(bu)保證有的特色(se)符(fu)號造(zao)成失效哦!正常(chang)是99%是無錯的
第二種寫(xie)法(fa):不用JS
打(da)開(kai)\extend\function.php
在上(shang)面(mian)PHP基礎(chu)上(shang)面(mian) 再新增:以下代碼:
function trim_extra_after_ext($url) {
$extensions = ['pdf', 'rar', 'zip', 'doc', 'docx', 'xls', 'xlsx'];
$pattern = '/(' . implode('|', array_map(function($ext) {
return preg_quote($ext, '/');
}, $extensions)) . ')(\?.*)?$/i';
if (preg_match($pattern, $url, $matches)) {
$ext = $matches[1];
return substr($url, 0, strpos(strtolower($url), $ext) + strlen($ext));
}
return $url;
}
最后模板的標簽為:{$eyou.field.aid|get_dopdf_value|trim_extra_after_ext}
正常調用
內容(rong)頁: <a href="{$eyou.field.aid|get_dopdf_value|trim_extra_after_ext}">{$eyou.field.aid|get_dopdf_value|trim_extra_after_ext}</a>
列表(biao)頁 : <a href="{$field.aid|get_dopdf_value|trim_extra_after_ext}">{$field.aid|get_dopdf_value|trim_extra_after_ext}</a>