loop - Display all the terms within a custom taxonomy

admin2025-01-08  5

I am looking to make a few different displays on the home page of terms within certain taxonomies as well as their image field as defined by ACF.

I have been looking online and found 2 main different ways to do this and would like to initially ask what is the difference between the two:?

WP_Term_Query();

and

get_terms();

This is the code I have so far but it does not seem to work with the_title() and the_permalink().

Also, it displays the ACF image field of the first result but then repeats the same image for the others. The ACF custom image field on the term is 'artist_thumbnail_image'

<?php
$args = array(
    'taxonomy'               => 'artist',
    'orderby'                => 'name',
    'order'                  => 'ASC',
    'hide_empty'             => false,
    'meta_key'               => 'artist_featured',
    'meta_value'             => '1',
    'meta_compare'           => '==',
);
$the_query = new WP_Term_Query($args);
foreach ($the_query->get_terms() as $term) { ?>
    <?php echo $term->name; ?>

    <?php
    $featuredartistid = $term->term_id;
    $featuredartist = get_field('artist', $featuredartistid);
    $featuredartistthumbnailimage = get_field('artist_thumbnail_image', $featuredartist);
    $featuredartistthumbnailimagesize = 'thumbnail'; ?>

    <a href="<?php echo the_permalink(); ?>">
        <?php echo wp_get_attachment_image($artistthumbnailimage, $artistthumbnailimagesize); ?>
    </a>

<?php } ?>

I am looking to make a few different displays on the home page of terms within certain taxonomies as well as their image field as defined by ACF.

I have been looking online and found 2 main different ways to do this and would like to initially ask what is the difference between the two:?

WP_Term_Query();

and

get_terms();

This is the code I have so far but it does not seem to work with the_title() and the_permalink().

Also, it displays the ACF image field of the first result but then repeats the same image for the others. The ACF custom image field on the term is 'artist_thumbnail_image'

<?php
$args = array(
    'taxonomy'               => 'artist',
    'orderby'                => 'name',
    'order'                  => 'ASC',
    'hide_empty'             => false,
    'meta_key'               => 'artist_featured',
    'meta_value'             => '1',
    'meta_compare'           => '==',
);
$the_query = new WP_Term_Query($args);
foreach ($the_query->get_terms() as $term) { ?>
    <?php echo $term->name; ?>

    <?php
    $featuredartistid = $term->term_id;
    $featuredartist = get_field('artist', $featuredartistid);
    $featuredartistthumbnailimage = get_field('artist_thumbnail_image', $featuredartist);
    $featuredartistthumbnailimagesize = 'thumbnail'; ?>

    <a href="<?php echo the_permalink(); ?>">
        <?php echo wp_get_attachment_image($artistthumbnailimage, $artistthumbnailimagesize); ?>
    </a>

<?php } ?>
Share Improve this question asked Mar 19, 2020 at 20:13 iamonstageiamonstage 1671 silver badge9 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0
  1. get_terms() creates a WP_Term_Query() and then passed the $args to it. It also applies the get_terms filter to the terms before they're returned. get_terms() is, in this case, a convenience function, to use if you don't need all the bells and whistles a WP_Term_Query might provide.

  2. As far as the_title() and the_permalink(), those functions are meant to work on posts, not taxonomy terms. the_title() will only have meaning if you're in The Loop, I believe. Outside The Loop, you can use get_the_title(), but you'll need to pass it a post or a post ID.

  3. Your image problem might be because you're changing from featuredartistthumbnailimage and featuredartistthumbnailimagesize to artistthumbnailimage and artistthumbnailimagesize in your code.

Got it. My issues were the incorrect featuredartistthumbnailimagesize to artistthumbnailimage

Plus also I should have been using the $term variable passed to the wp_get_attachment_image. Not the $term->id.

Below is my working code. Hopefully it will help someone.

$fa_args = array(
    'taxonomy'               => 'artist',
    'hide_empty'             => false,
    'meta_key'               => 'artist_featured',
    'meta_value'             => '1',
);
$fa_query = new WP_Term_Query($fa_args);
if (!empty($fa_query->terms)) {
    foreach ($fa_query->terms as $fa_term) {

        $fa_id = $fa_term->term_id; // Term ID
        $fa_link = get_term_link($fa_id); // The term link
        $fa_image_id = get_field('artist_thumbnail_image', $fa_term); //Artist Thumbnail Image ID
        $fa_image = wp_get_attachment_image($fa_image_id, 'thumbnail');

        echo $fa_term->name;
        echo '<br>';
        echo $fa_image;
        echo '<br>';

    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266151a1097.html

最新回复(0)