最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Getting custom taxonomy from custom post type

programmeradmin2浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 37

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?

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;
        ?>
发布评论

评论列表(0)

  1. 暂无评论