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; ?>
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; ?>
Share
Improve this question
asked Jul 2, 2014 at 18:16
imzimz
1431 gold badge1 silver badge5 bronze badges
2 Answers
Reset to default 37You 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?
this is best way to do it
<?php
$taxonomy = 'movies-category';
$terms = get_object_term_cache( $post->ID, $taxonomy );
$output = '';
foreach($terms as $term) {
if(!empty($output))
$output .= ' | ';
$output .= '<span class="cat"><a href="'. esc_url( get_term_link( $term )). '">'.$term->name.'</a></span>';
}
echo $output;
?>