生成动态term_id-返回图像字段-ACF

时间:2018-10-04 12:40:49

标签: php wordpress advanced-custom-fields

这可行,除了我想动态生成$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); 
?>

1 个答案:

答案 0 :(得分:0)

您的问题对生成term_id不太清楚。

您需要确定生成term_id的标准。要么应该基于Post ID或其他ID。

如果ypou要分配发布ID,则可以这样做

    global $post;

$term_id =  $post->ID;

但是您可以使用uniqid()生成随机数

就像$term_id = uniqid();

OR

您可以将rand()pow()结合使用来实现此目的:

$digits = 3;
echo rand(pow(10, $digits-1), pow(10, $digits)-1);

这将输出一个介于100和999之间的数字。这是因为10 ^ 2 = 100和10 ^ 3 = 1000,然后您需要将其减去一个,以使其处于所需的范围内。

如果005也是有效的示例,则可以使用以下代码在其前添加零:

$digits = 3;
echo str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);
相关问题