3

I have a custom post type called email_block and have a custom taxonomy called block_type. I need to loop through all the email blocks find out what custom taxonomy (block_type) they have. I know how get all the email block custom post types, it's finding the what block_type they belong to is what I'm struggling with.

This is the code I have so far. I'm using a relationship field from advanced custom fields to filter what email blocks I want to display.

<?php

$posts = get_field('block_selector');

if( $posts ): ?>
    <ul>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <span>Custom field from $post: <?php the_field('author'); ?></span>
        </li>
    <?php endforeach; ?>
    </ul>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly 
    endif; ?>
17

You mean get_the_terms()?

<?php 
    $terms = get_the_terms( $post->ID, 'block_type' ); 
    foreach($terms as $term) {
      echo $term->name;
    }
?>

Or have I simplified this too much?

  • No that was exactly what I was looking for! Thanks :) – imz Jul 2 '14 at 22:12

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.