Skip to main content
1 of 3
Mr. B
  • 307
  • 5
  • 14

Content Above the post title, but not on tag or category pages

I have the following code working perfectly right now. It grabs info from posts based on the tag found in the page. I'd like to modify it so it grabs the same info, but based on the category, not the tag.

I reviewed the instructions here = http://codex.wordpress.org/Function_Reference/get_the_category but I'm doing something wrong.

Can someone help me tweak this so it grabs info based on 'category' instead of 'tag'?

 /**
     * tag related posts
     */
    function relatedPosts(){
    
        $tags = get_the_tags();
    if ( $tags ) {
        foreach ( $tags as $t ) {
            $tag = $t->name;
            break;
        }
    }
        $posts = get_posts('numberposts=5&orderby=rand&fields=all&tag='.$tag);
    
        $html = '';
    if ( $posts ) {
        foreach ( $posts as $post ) {
            $meta = get_post_meta( $post->ID );
            $image = $meta['og_image'][0];
            $html .= '<a href="http://XXXXX.com/'.$post->post_name.'/"><img src="'.$image.'"  class="alignnone"  /></a>';
        }
    }
        return do_shortcode($html);
    
    }
    
    
    add_shortcode('related', 'relatedPosts');
Mr. B
  • 307
  • 5
  • 14