想要获取自定义字段的附件类型里面的数据地址,只能以自定义形式。当然以下办法是比较笨的也不是很完美,但基础达到目的就行了。
有用就使用吧!

效果是 获取自定义字段的附件类型 实际路径。
操作方法:
打开\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>