Support

Account

Home Forums Front-end Issues Show category name and image on related custom post

Solving

Show category name and image on related custom post

  • Hi there.

    I have set up a relationship between the post type opportunity (as in job opportunity) and linked organisation – the organisation that is offering the opportunity.

    There is a custom taxonomy for the Organisation called Accreditation which shows the various 3rd party accreditation that organisation has.

    My question is

    a) how can I show the name of the related organisation’s accreditations on the opportunity post

    b) show the ACF custom fields for logo and url that I have attached to the accreditation terms.

    Currently I am showing the custom field for organisation location on the opportunity template using the following query, which I hope to expand to include the accreditation category and its associated custom fields.


    $linked_organisation = get_field('linked_organisation');
    if ($linked_organisation):
    foreach( $linked_organisation as $l) :
    $organisation_location = get_field('organisation_town', $l->ID);
    endforeach;
    endif;

    Thanks in advance,

    Mat

  • You need to get the taxonomy terms associated with the related posts and loop over them

    
    <?php 
    $linked_organisation = get_field('linked_organisation');
    if ($linked_organisation) {
      foreach ($linked_organisation as $l) {
        $terms = get_post_terms($l->ID, 'YOUR TAXOMY HERE');
        if ($terms) {
          foreach ($terms as $term) {
            // get fields from term
            get_field('FIELD NAME', $term);
          }
        }
      }
    }
    
  • Hi John – thanks for your reply, sorry about the delay but I’ve been a bit unwell recently. Back on track now but still can’t get this code to work. I get the error “Uncaught Error: Call to undefined function get_post_terms()…”

    This is the code I’ve used

    <?php 
    $linked_organisation = get_field('linked_organisation');
    if ($linked_organisation) {
    foreach ($linked_organisation as $l) {
    $terms = get_post_terms($l->ID, 'accreditations');
    if ($terms) {
    foreach ($terms as $term) {
    // get fields from term
    $caturl = get_field('accreditation_url', $term);
    $catname = $term->name;
    $catimg = get_field('accreditation_logo', $term);
    $catimgurl =  $catimg['url'];
    $catimgalt =  $catimg['alt'];
    $size = 'full'; // (thumbnail, medium, large, full or custom size)
    ?>
    <div>
    	<?php	if ( empty( $catimgurl ) ): ?>
    <h3><?php echo $catname; ?></h3>
    		<?php endif; ?>
    <?php if( $caturl ): ?>
    	<a href="<?php echo $caturl; ?>">
    	<?php endif; ?>
    <?php	if ( !empty( $catimgurl ) ) { ?>
    	<img src="<?php echo $catimgurl; ?>"
    	alt="<?php echo $catimgalt; ?>" />
    		<?php if( $caturl ): ?>
    	</a>
    	<?php endif; ?>
    	<?php } ?>
    
    </div>
    <?php ;
    }}}}
    ?>

    Anything you can suggest would be appreciated. Thanks, Mat

  • I got the function name wrong, it should be wp_get_post_terms()

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

You must be logged in to reply to this topic.