PHP Code to Display Term URL & Name of a Post Under a Custom Post Type

I am featuring a post in my footer(outside the loop) from a custom post type and am using Advanced Custom Fields plugin to do so. This is the code I am using:

<h5>Featured Movie</h5>
<?php query_posts(array(
'posts_per_page' => 1,
'post_type' => 'movies',
'orderby' => 'post_date',
'meta_key' => 'featured_movie',
'meta_compare' => '=',
'meta_value' => 1,
'paged' => $paged
)
); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="popcontainer">
<div class="popthumb"><?php the_post_thumbnail('full-thumbnail');?></div>
<div class="clear"></div>
"<?php the_content(); ?>"
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

While that does exactly what I need it to, I also need some code in there that displays & links to the term(s) that are associated with whatever post being featured at the moment. However the code I have asks me to specify a taxonomy and I dont think that’s the way to go as it’s too limiting & I want to be able to pull from every post in the movies custom post type regardless of what taxonomies and terms they have. The code I have right now is

Read More
<?php   // Get terms for post
 $terms = get_the_terms( $post->ID , 'genre' );
 // Loop over each item since it's an array
 if ( $terms != null ){
 foreach( $terms as $term ) {
$term_link = get_term_link( $term, 'genre' );
 // Print the name method from $term which is an OBJECT
echo '<li><a href="' . $term_link . '">' . $term->name . '</a></li>';
 // Get rid of the other data stored in the object, since it's not needed
 unset($term);
} } ?>

As you can see I had to specify genre as the taxonomy for the sake of having it show up on my site & for the sake of this example but I dont want to specify a taxonomy bc doing so leaves out every other taxonomy I have like ratings or release date. Sure I could put every single one of my taxonomies into the code to make sure they’re all included but there has to be an easier way. Further, how do I make sure that every term associated with the post is displayed.

Related posts

1 comment

  1. Use get_object_taxonomies to get all of the taxonomies registered to a particular post type.

    Also, don’t use query_posts for secondary queries. or ever, actually. Use WP_Query.

    $args = array(
        'posts_per_page' => 1,
        'post_type' => 'movies',
        'orderby' => 'post_date',
        'meta_key' => 'featured_movie',
        'meta_compare' => '=',
        'meta_value' => 1,
    );
    $movie = new WP_Query( $args );
    
    if( $movie->have_posts() ){
        while( $movie->have_posts() ){
            $movie->the_post();
    
            // your post template tags
            the_title();
            the_post_thumbnail('full-thumbnail');
    
            // taxonomies/terms
            if( $taxonomies = get_object_taxonomies( 'movies' ) ){
                foreach( $taxonomies as $taxonomy ){
                    if( $terms = get_the_terms( $post->ID , $taxonomy ) ){
                        foreach( $terms as $term ){
                            echo '<li><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';
                        }
                    }
                }
            }
        }
        wp_reset_postdata();
    }
    

Comments are closed.