使用 ACF 从分类术语中获取图像
Get image from taxonomy term using ACF
我正在使用以下代码尝试使用高级自定义字段插件从分类术语中检索名为 'image' 的图像字段。此代码基于 ACF website here.
上的文档
应该注意的是,此代码在 taxonomy.php 模板中使用,我无法指定特定的分类法 and/or 术语,因为我需要代码来检测当前的分类法和术语,基于用户点击进入的页面。
非常感谢任何帮助!
<?php get_header(); ?>
<?php get_sidebar(); ?>
<section id="hero-image">
<div class="gradient-overlay">
<?php
// vars
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
// load thumbnail for this taxonomy term (term object)
$image = get_field('image', $queried_object);
// load thumbnail for this taxonomy term (term string)
$image = get_field('image', $taxonomy . '_' . $term_id);
?>
</div>
<div class="grid">
<header class="unit full-width">
<a href="<?php echo home_url(); ?>/" title="Kurdistan Memory Programme" class="logo"><?php bloginfo( 'name' ); ?></a>
</header>
<footer class="unit one-half">
<h1><?php single_cat_title(); ?></h1>
<h4 class="scroll-down">Scroll down to continue</h4>
</footer>
</div>
</section>
<?php get_footer(); ?>
taxonomy.php 是基于 archives.php 相同模型的自定义分类法模板
在这种模板中,你应该使用wordpress循环
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
他们,你可以传入get_field,像这样的post ID作为第二个参数:
$image = get_field('image', $post->ID);
好的,所以你得到了字段的值,你只需要设置它应该如何输出,就像这样:
$image = get_field('image', $taxonomy . '_' . $term_id);
echo '<img src="'.$image['sizes']['thumbnail'].'" alt="$image['alt']" />';
这假定您要使用缩略图大小。如果使用不同的大小,请将该文本更改为适当的图像大小。
如果要return全尺寸图片,请使用以下代码:
$image = get_field('image', $taxonomy . '_' . $term_id);
echo '<img src="'.$image['url'].'" alt="$image['alt']" />';
<?php
$terms = get_field('best_seller_laptops_pc_category');
if( $terms ): ?>
<?php foreach( $terms as $term ):
$thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
$term_img = wp_get_attachment_url( $thumb_id );
?>
<div class="col-lg-6">
<div class="addbox1">
<img alt="" src="<?php echo $term_img;?>">
<div class="contain">
<h3>
<?php echo esc_html( $term->name ); ?>
</h3>
<h4><?php echo esc_html( $term->description ); ?></h4>
<a href="<?php echo get_term_link( $term ); ?>">LEARN MORE</a>
<a href="<?php echo get_term_link( $term ); ?>" class="btn">buy now</a>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
我正在使用以下代码尝试使用高级自定义字段插件从分类术语中检索名为 'image' 的图像字段。此代码基于 ACF website here.
上的文档应该注意的是,此代码在 taxonomy.php 模板中使用,我无法指定特定的分类法 and/or 术语,因为我需要代码来检测当前的分类法和术语,基于用户点击进入的页面。
非常感谢任何帮助!
<?php get_header(); ?>
<?php get_sidebar(); ?>
<section id="hero-image">
<div class="gradient-overlay">
<?php
// vars
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
// load thumbnail for this taxonomy term (term object)
$image = get_field('image', $queried_object);
// load thumbnail for this taxonomy term (term string)
$image = get_field('image', $taxonomy . '_' . $term_id);
?>
</div>
<div class="grid">
<header class="unit full-width">
<a href="<?php echo home_url(); ?>/" title="Kurdistan Memory Programme" class="logo"><?php bloginfo( 'name' ); ?></a>
</header>
<footer class="unit one-half">
<h1><?php single_cat_title(); ?></h1>
<h4 class="scroll-down">Scroll down to continue</h4>
</footer>
</div>
</section>
<?php get_footer(); ?>
taxonomy.php 是基于 archives.php 相同模型的自定义分类法模板 在这种模板中,你应该使用wordpress循环
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
他们,你可以传入get_field,像这样的post ID作为第二个参数:
$image = get_field('image', $post->ID);
好的,所以你得到了字段的值,你只需要设置它应该如何输出,就像这样:
$image = get_field('image', $taxonomy . '_' . $term_id);
echo '<img src="'.$image['sizes']['thumbnail'].'" alt="$image['alt']" />';
这假定您要使用缩略图大小。如果使用不同的大小,请将该文本更改为适当的图像大小。
如果要return全尺寸图片,请使用以下代码:
$image = get_field('image', $taxonomy . '_' . $term_id);
echo '<img src="'.$image['url'].'" alt="$image['alt']" />';
<?php
$terms = get_field('best_seller_laptops_pc_category');
if( $terms ): ?>
<?php foreach( $terms as $term ):
$thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
$term_img = wp_get_attachment_url( $thumb_id );
?>
<div class="col-lg-6">
<div class="addbox1">
<img alt="" src="<?php echo $term_img;?>">
<div class="contain">
<h3>
<?php echo esc_html( $term->name ); ?>
</h3>
<h4><?php echo esc_html( $term->description ); ?></h4>
<a href="<?php echo get_term_link( $term ); ?>">LEARN MORE</a>
<a href="<?php echo get_term_link( $term ); ?>" class="btn">buy now</a>
</div>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>