Home Forums General Issues Image Custom Field from CPT converted to Taxonomy


Image Custom Field from CPT converted to Taxonomy

  • I have a CPT with slug of ‘teammember’ that is being converted to taxonomy by CPT-onomies plugin and attached to another CPT ‘event’. The CPT ‘teammember’ has an image custom field ‘headshot’. I am using a post data custom field called ‘special’ on a page to allow the author to pick an ‘event’ to display on this page. Along with custom fields from the chosen ‘event’, I want to include the ‘headshot’ from the chosen ‘teammember ‘ of that ‘event’. I can not seem to make this work. I can get regular term data from the selected ‘teammember’ like title, but not a custom field.

    Below is a simplified version of the code I am trying…

        $post_object = get_field('special');
        if( $post_object ):
            $post = $post_object;
            setup_postdata( $post );
        if ( $team_members = wp_get_post_terms( $post->ID, 'teammember' ) )
            $author = $team_members[0];
            $permalink = get_permalink( $author->term_id );
            if ($author)
                print '<a href="' . $permalink . '">' . $author->name . '</a>';
    <?php $image = get_field('headshot'); ?>
    <a href="<?php the_permalink(); ?>">
    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
    <?php the_title(); ?>, <?php the_date(); wp_reset_postdata(); ?>

    Any ideas or guidance is appreciated -thanks!

  • Hi @mcseay

    CPT-onomies has a weird structure as it forces a post as a taxonomy. To get the custom field, you need to pass the id of this post (the post that is converted to a taxonomy) as the second parameter. Something like this:

    <?php $image = get_field('headshot', $cptonomies_post_id); ?>

    where $cptonomies_post_id is the id of the post.

    I hope this makes sense.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Image Custom Field from CPT converted to Taxonomy’ is closed to new replies.