Timber Twig 从 ACF 字段获取文件的文件大小
Timber Twig get the filesize of a file from ACF field
我有一个文件上传的 ACF 字段。我能够获得附件 ID。但是,在我的树枝模板中,我无法输出文件大小,因为这无法通过 ACF 获得。
我找到了一个函数:
$context['filesize'] = (filesize( get_attached_file(the_attachment_id_here))/1000 );
但我真正需要的是它在树枝循环中。
我已经在我的模板中试过了:
{% for item in posts %}
{{function(filesize( get_attached_file(item.get_field('pdf').id))/1000 )}}
{% endfor %
但它 returns 一个错误文件大小未知函数。
任何帮助都会很棒!
哎呀,真讨厌 —
真正的最好的方法是...
首先为您的帖子创建自定义 class。添加一种方法来执行您正在寻找的获取和转换...
class MyPost extends \Timber\Post {
function pdf_filesize() {
$pdf = $this->get_field('pdf');
//you may need to convert the value of $pdf into an array...
$file = get_attached_file($pdf['id']);
return filesize($file) / 1000;
}
}
然后在任何你获取帖子的地方,使用 MyPost
代替...
$context['posts'] = Timber::get_posts(null, 'MyPost');
在 Twig 文件中...
{% for post in posts %}
File size is {{ post.pdf_filesize }}
{% endfor %}
如果你只是需要破解它...
{% for item in posts %}
{{ function(
'filesize', function(
'get_attached_file', item.get_field('pdf').id
)
) / 1000 }}
{% endfor % }
A var_dump
表示文件大小是
echo '<pre>';
var_dump( $file );
echo '</pre>';
输出文件大小
$file = get_field('file');
$filesize = $file['filesize'];
$filesize = size_format($filesize, 2);
我有一个文件上传的 ACF 字段。我能够获得附件 ID。但是,在我的树枝模板中,我无法输出文件大小,因为这无法通过 ACF 获得。
我找到了一个函数:
$context['filesize'] = (filesize( get_attached_file(the_attachment_id_here))/1000 );
但我真正需要的是它在树枝循环中。
我已经在我的模板中试过了:
{% for item in posts %}
{{function(filesize( get_attached_file(item.get_field('pdf').id))/1000 )}}
{% endfor %
但它 returns 一个错误文件大小未知函数。
任何帮助都会很棒!
哎呀,真讨厌 —
真正的最好的方法是...
首先为您的帖子创建自定义 class。添加一种方法来执行您正在寻找的获取和转换...
class MyPost extends \Timber\Post {
function pdf_filesize() {
$pdf = $this->get_field('pdf');
//you may need to convert the value of $pdf into an array...
$file = get_attached_file($pdf['id']);
return filesize($file) / 1000;
}
}
然后在任何你获取帖子的地方,使用 MyPost
代替...
$context['posts'] = Timber::get_posts(null, 'MyPost');
在 Twig 文件中...
{% for post in posts %}
File size is {{ post.pdf_filesize }}
{% endfor %}
如果你只是需要破解它...
{% for item in posts %}
{{ function(
'filesize', function(
'get_attached_file', item.get_field('pdf').id
)
) / 1000 }}
{% endfor % }
A var_dump
表示文件大小是
echo '<pre>';
var_dump( $file );
echo '</pre>';
输出文件大小
$file = get_field('file');
$filesize = $file['filesize'];
$filesize = size_format($filesize, 2);