php - 生成动态 term_id - 返回图像字段 - ACF
问题描述
这可行,除了我想$term_id = 22;
动态生成,所以它是每个产品的正确“品牌”。
<?php
$taxonomy_prefix = 'brand';
$term_id = 22;
$term_id_prefixed = $taxonomy_prefix .'_'. $term_id;
$brand_logo = get_field( 'brand-logo', $term_id_prefixed );
if ( $brand_logo ) { ?>
<img src="<?php echo $brand_logo['url']; ?>" />
<?php }
?>
这里的代码适用于文本字段(品牌信息) -$term_id
每个帖子都是动态的..希望它进一步澄清我的问题。
<?php
$terms = get_the_terms( $post->ID , 'brand' );
$term_id = $terms[0]->term_id;
the_field('brand info', 'term_'.$term_id);
?>
解决方案
您的问题对生成 term_id 不是很清楚。
您需要确定生成 term_id 的标准。它应该基于 Post Id 或其他一些 ID。
如果你想分配 Post Id,那么你可以这样做
global $post;
$term_id = $post->ID;
但是,您可以使用 uniqid() 生成随机数
喜欢,$term_id = uniqid();
或者
$digits = 3;
echo rand(pow(10, $digits-1), pow(10, $digits)-1);
这将输出一个介于 100 和 999 之间的数字。这是因为 10^2 = 100 和 10^3 = 1000,然后您需要将其减去 1 以使其处于所需范围内。
如果 005 也是一个有效示例,您将使用以下代码用前导零填充它:
$digits = 3;
echo str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);
推荐阅读
- python - 在给定交叉矩阵的情况下查找不同的互联网提供商
- r - “plot.xy(xy.coords(x, y), type = type, ...) 中的错误:尚未调用 plot.new”
- html - 当文本在高度上溢出div时如何显示3个点
- javascript - 将输入与动态加法相乘
- python-3.x - Django 在调用 save() 时更新模型而不是插入
- excel - 将单元格中的前 3 个字符固定为整列,例如 PRT,这些单元格中的任何新输入都将在 PRT 之后填充,因此 PRT1253
- angular - 用于注入其他服务的服务的 Angular 单元测试
- python - 如何正确转换日期时间以将其用作文件的标题?
- phpspreadsheet - phpspreadsheet中的辅助y轴
- python - 如何使用 Pyrogram 查看存档聊天