-1

I have a custom post type called “project” and a list of 8 CPT categories. On the homepage of my site, I want to call the latest post and echo the project name, excerpt, and category. I have the name and excerpt working, but I cannot figure out how to pull in the custom taxonomy.

I know I can use wp_get_post_terms() to get taxonomy terms assigned to a specific post, but this returns an array of term objects. I don't know how to loop through the return and echo out $term->name each time.

<?php
$args = array(
    'meta_key'     => 'featured',
    'meta_value'   => '1',
    'post_type' => 'project'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
    $query->the_post();
    $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' ); ?>

    <div class="cpt-feature" style="background: url('<?php echo $backgroundImg[0]; ?>') center center no-repeat; background-size: cover;">
    <?php echo get_the_post_thumbnail( $page->ID, 'thumbnail' ); ?>

    <?php
    echo "<div class='cpt-overlay'>";
    echo "<div class='project-information'>";
    // NEED TO INSERT CPT CATEGORY HERE
    echo "<h3>";
        the_title();
    echo "</h3>";
    echo "<p>";
    echo get_field('intro_blurb');
    echo "<p><a href='" . get_permalink() . "'>View Project</a></p>";
    echo "</div>";
    echo "</div>";
    echo "</a>";
}
wp_reset_postdata(); } else { }?>

1 Answers1

0

You can loop through the return value of wp_get_post_terms() pretty easily, with a simple foreach loop:

$terms = wp_get_post_terms( $post->ID, 'your-taxonomy-here' );

foreach( $terms as $term){
    echo $term->name;
}

However you may not even need to do that if you just need a simple linked list, you can use get_the_term_list() instead.

Another note is that you don't need a single echo per line. In this case it might be better to escape PHP and use the self-echoing functions available in WP. And help future you now by working on your indentation and formatting. Here's how I'd approach this with get_the_term_list():

<?php
    $args = array(
        'meta_key'   => 'featured',
        'meta_value' => '1',
        'post_type'  => 'project'
    );

    $query = new WP_Query( $args );

    if( $query->have_posts() ){
        while( $query->have_posts() ){
            $query->the_post();

            $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>

            <div class="cpt-feature" style="background: url('<?= $backgroundImg[0]; ?>') center center no-repeat; background-size: cover;">
                <?php the_post_thumbnail( $page->ID, 'thumbnail' ); ?>
                <div class="cpt-overlay">
                    <div class="project-information">
                        <?php
                            echo get_the_term_list( $post->ID, 'your-taxonomy-here' );
                            the_title( '<h3>', '</h3>' );
                        ?>
                        <p>
                            <?php the_field( 'intro_blurb' ); ?>
                            <a href="<?php the_permalink(); ?>">View Project</a>
                        </p>
                    </div>
                </div>
            </div>
        <?php }
        wp_reset_postdata();
    }
?>

Or if you don't want a linked list, and instead want to use wp_get_post_terms() it's simply a matter of swapping the functions and adding a foreach:

<?php
    $args = array(
        'meta_key'   => 'featured',
        'meta_value' => '1',
        'post_type'  => 'project'
    );

    $query = new WP_Query( $args );

    if( $query->have_posts() ){
        while( $query->have_posts() ){
            $query->the_post();

            $backgroundImg = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>

            <div class="cpt-feature" style="background: url('<?= $backgroundImg[0]; ?>') center center no-repeat; background-size: cover;">
                <?php the_post_thumbnail( $page->ID, 'thumbnail' ); ?>
                <div class="cpt-overlay">
                    <div class="project-information">
                        <?php
                            foreach( wp_get_post_terms( $post->ID, 'your-taxonomy-here' ) as $term )
                                echo $term->name;

                            the_title( '<h3>', '</h3>' );
                        ?>
                        <p>
                            <?php the_field( 'intro_blurb' ); ?>
                            <a href="<?php the_permalink(); ?>">View Project</a>
                        </p>
                    </div>
                </div>
            </div>
        <?php }
        wp_reset_postdata();
    }
?>
Xhynk
  • 12,250
  • 8
  • 29
  • 61