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

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

這2個改了 就(jiu)可以,合并模板調用標(biao)簽就(jiu)是:
{$eyou.field.aid|get_dopdf_value}
控制頁的就完成了。
同樣你有多個(ge)模(mo)型(xing) 再(zai)安裝上面方法(fa)添加一個(ge)即可(ke) 當然你要把(ba)get_dopdf_value 這個(ge) 修改(gai)為其他自己隨意即可(ke) 標簽就是 {$eyou.field.aid|自定(ding)義}
以下是具體:
if (!function_exists('自定義函(han)數(shu)'))
{
/**
* 查詢當前文章的(de)dopdf字段的(de)值
*/
function 自定義函數($aid = 0)
{
// 定義查(cha)詢條件
$condition = ['a.aid' => $aid];
// 查詢當前文章的dopdf字段(duan)的值
$record = \think\Db::name('模型內(nei)容表')
->alias('a')
->where($condition)
->value('自(zi)定義字段名(ming)稱');
return $record;
}
}
第(di)二部在 前(qian)端模(mo)板(ban) 添(tian)加(jia)以下代碼:
<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說明(ming) 紅色字為類型,如果你是(shi)其(qi)它比如rar 就改下 .rar 即(ji)可 以上(shang)不保(bao)證有的(de)特(te)色符號造(zao)成(cheng)失效(xiao)哦!正常(chang)是(shi)99%是(shi)無錯的(de)
第(di)二(er)種寫法:不用JS
打開\extend\function.php
在上(shang)面(mian)PHP基(ji)礎上(shang)面(mian) 再新增:以(yi)下代(dai)碼(ma):
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;
}
最后模板的(de)標簽為:{$eyou.field.aid|get_dopdf_value|trim_extra_after_ext}
正常調用
內(nei)容頁: <a href="{$eyou.field.aid|get_dopdf_value|trim_extra_after_ext}">{$eyou.field.aid|get_dopdf_value|trim_extra_after_ext}</a>
列表頁 : <a href="{$field.aid|get_dopdf_value|trim_extra_after_ext}">{$field.aid|get_dopdf_value|trim_extra_after_ext}</a>