• Resolved jodineuf

    (@jodineuf)


    First of all, thank you so much for your wonderful plugin and for the amazing support you provide!

    I have a CPT-onomy called “People” that I’m using on my posts.

    I have created a custom taxonomy for People called “Institutions”

    I have created a custom field for People called “URL”

    On the sidebar for each individual post (in single.php), I need to display an alphabetical list of all “People” related to the post and include the following information for each Person:

    • Person’s featured image
    • Person’s title hyperlinked with person’s “URL”
    • Person’s “Institution”

    I’ve been hacking away at this for about two days now, and so far I’ve only come up with:

    <?php
    $people_list = get_the_terms( $post->ID, 'people' );
    if ( $people_list && ! is_wp_error( $people_list ) ) :
    ?><?php
    foreach ( $people_list as $term ) {
    ?><p><?php
    if ( has_post_thumbnail( $term->term_id ) && ( $image = wp_get_attachment_image_src( get_post_thumbnail_id( $term->term_id ), 'full' ) ) && isset( $image[0] ) ) {
    ?><img src="<?php echo $image[0]; ?>" class="alignleft" /><a href="#"><?php
    }
    echo $term->name;
    ?></a></p>
    <?php
    }
    ?>
    <?php endif; ?>

    This is of course based on your extremely helpful example from the documentation on your site. This code takes care of the featured image and the title, but I’m stuck now on how to add the link to the URL and the institutions. Can you give me some hints on how to accomplish this?

    Unfortunately I’m not able to make my site public yet, so I can’t provide a link for reference.

    Thank you very much for all you do to make this amazing plugin!

    https://www.ads-software.com/plugins/cpt-onomies/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter jodineuf

    (@jodineuf)

    After reading through to the older threads on this forum, I saw this one that might get me closer to what I’m looking to do, although I still don’t find the way to add my custom taxonomy terms (Institution) or link the title to the custom field value (URL).

    <?php
    // retrieve the IDs of People related to this post
    $people_list= wp_get_object_terms( get_the_ID(), 'people', array( 'fields' => 'ids' ) );
    
    // use "the Loop" to retrieve people info, but we only want the people with particular IDs ('post__in')
    $people_list= new WP_Query( array( 'post_type' => 'people', 'post__in' => $people_list, 'orderby' => 'title', 'order' => 'ASC', 'showposts' => -1 ) );
    
    // print the people
    if ( $people_list->have_posts() ) :
       while( $people_list->have_posts() ) : $people_list->the_post();
          the_post_thumbnail('thumbnail', array('class' => 'alignleft'));
       //here is where I want to link to the custom field value URL
       the_title();
       //here is where I want to have the terms from Institutions
       endwhile;
    endif;
    ?>

    Am I right that this method is better for what I’m trying to do?

    Thanks!!

    I think your original approach should work just fine. You just need to retrieve the URL and the institutions:

    <?php
    global $cpt_onomy;
    $people_list = get_the_terms( $post->ID, 'people' );
    if ( $people_list && ! is_wp_error( $people_list ) ) {
       foreach ( $people_list as $term ) {
          ?><p><?php
             if ( has_post_thumbnail( $term->term_id ) && ( $image = wp_get_attachment_image_src( get_post_thumbnail_id( $term->term_id ), 'full' ) ) && isset( $image[0] ) ) {
                ?><img src="<?php echo $image[0]; ?>" class="alignleft" /><a href="#"><?php
             }
             if ( $url = get_post_meta( $term->term_id, 'URL', true ) ) {
                ?><a href="echo $url; ?>"><?php echo $term->name; ?></a><?php
             } else
                echo $term->name;
          ?></p><?php
          echo $cpt_onomy->get_the_term_list( $term->term_id, 'institutions', 'Institution: ', ', ', '' );
       }
    }
    ?>

    If get_the_term_list() isn’t what you need, you could use get_the_terms() again. Hope this helps.

    Thread Starter jodineuf

    (@jodineuf)

    Thanks so much for your reply!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display custom taxonomy terms for CPT-onomy on a single post page’ is closed to new replies.