2

I am in the single-product.php loop and the product has a relationship to an artist post type to which I have the id off the artist.

I need to get the_title() from my custom artist post type while in the single-product.php loop.

My code is getting the title of the product rather than the artist. My code is below. Can anyone please help?

    $artistId = get_field('artist');

    $postId = get_post($artistId);
    if ( $postId ):
        setup_postdata($postId);
        ?>
        <span id="chty_17">
            <dt><?php the_title(); ?></dt>
        </span>
        <?php
        wp_reset_postdata();
    endif;

1 Answer 1

2

You can pass a post ID to get_the_title(). So instead of using the_title() to display it, fetch it first like this.

$artist_title = get_the_title( $artistID );
echo $artist_title;

You could do that in one line of course but you might need it somewhere else, too.

2
  • Great thank you! Will make sure I remember that in future! Sep 15, 2016 at 16:27
  • Happy to help and glad it worked for you.
    – jdm2112
    Sep 16, 2016 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.