Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

<?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.

share|improve this question
    
This is a much, much better formulated question than the other one :) –  s_ha_dum Nov 3 '13 at 2:50
    
This is a totally different issue and question though lol. I will repost the other when I figure out how to ask it. Seems I needed to already have some form of code in rotation for you to get the gist of what I was saying but since I don't I'm afraid when I repost it is gonna sound a lot like it did at first... –  digitaldoll Nov 3 '13 at 6:18

1 Answer 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();
}
share|improve this answer
    
Hello Milo! I tried your code and while it does show me every term associated with the post I am still getting an error: Warning: Invalid argument supplied for foreach() in /home/site-path-goes-here/footer.php on line 31 My actual post isn't showing but the terms associated with it are. –  digitaldoll Nov 3 '13 at 0:40
    
yeah sorry, I was just showing a quick example - the two foreachs should be wrapped in if( $taxonomies ) and if( $terms ) so you don't try to iterate over an empty set of data (like in your original example). You don't see any post data because there aren't any template tags in my example outputting it. add the_title() or the_content() in there to see your post title/content. –  Milo Nov 3 '13 at 0:56
    
Ok maybe I shouldve said I'm just above being a novice lol! I would totally appreciate you breaking it down so I wont mess anything up. I did try to paste in the_content() snippets from my original example up top after I realized they weren't in there but it didnt work & I am further frustrating myself lol.. am also not sure what you mean by putting if on the two foreach lines. I see it I just don't understand. –  digitaldoll Nov 3 '13 at 1:02
    
I just pasted if( $taxonomies ) directly before foreach( $taxonomies as $taxonomy ){ to get if( $taxonomies ) foreach( $taxonomies as $taxonomy ){ and then pasted if( $terms ) directly before foreach( $terms as $term ){ to get if( $terms ) foreach( $terms as $term ){ in your code and got no errors. Is this correct though? –  digitaldoll Nov 3 '13 at 2:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.