doushi1974 2015-12-10 16:03
浏览 53
已采纳

使用ACF从分类术语中获取图像

I am using the below code to try and retrieve an image field called 'image' from a taxonomy term using the Advanced Custom Fields plugin. This code is based on the documentation on the ACF website here.

It should be noted this code is being used within the taxonomy.php template, and I am unable to specify particular taxonomy and/or terms as I need the code to detect the current taxonomy and term, based on the page the user and clicked through to.

Any help much appreciated!

<?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(); ?>

enter image description here

  • 写回答

2条回答 默认 最新

  • douyou9923 2015-12-10 17:30
    关注

    Ok, so you are getting the value of the field, you just need to set how it should output, like so:

    $image = get_field('image', $taxonomy . '_' . $term_id);
    echo '<img src="'.$image['sizes']['thumbnail'].'" alt="$image['alt']" />';
    

    This assumes that you want to use the thumbnail image size. If using a different size, change that text to the appropriate image size.

    If you want to return the full size image, use the following code:

    $image = get_field('image', $taxonomy . '_' . $term_id);
    echo '<img src="'.$image['url'].'" alt="$image['alt']" />';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?