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

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

這2個改了 就可以,合并模板調用標簽就是:
{$eyou.field.aid|get_dopdf_value}
控制頁的就完成了。
同樣你有多個模型 再安裝上面方法添加一個即可 當然你要把get_dopdf_value 這個 修改為其他自己隨意即可 標簽就是 {$eyou.field.aid|自定義}
以下是具體:
if (!function_exists('自定義函數'))
{
/**
* 查詢當前文章的dopdf字段的值
*/
function 自定義函數($aid = 0)
{
// 定義查詢條件
$condition = ['a.aid' => $aid];
// 查詢當前文章的dopdf字段的值
$record = \think\Db::name('模型內容表')
->alias('a')
->where($condition)
->value('自定義字段名稱');
return $record;
}
}
第二部在 前端模板 添加以下代碼:
<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說明 紅色字為類型,如果你是其它比如rar 就改下 .rar 即可 以上不保證有的特色符號造成失效哦!正常是99%是無錯的
第二種寫法:不用JS
打開\extend\function.php
在上面PHP基礎上面 再新增:以下代碼:
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}
正常調用
內容頁: <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>