treehouse : what would you like to learn today?
Web Design Web Development iOS Development

get_post_meta() not working within a function

  • Hello everyone,

    I'm fairly new to coding, but I've been watching a lot of tutorials, many by Chris (which are awesome by the way!). I am trying to write my first function, and it works... sort of. What I want to do is create a function with a parameter of a category and number of posts to display, and then have that function display that particular category in the way it is marked up within the function (the html and whatnot). I have some meta data within a custom field in each post that I want to pull and display (an image to be exact). The function will display the text from the category and the number of posts I give it, but the image wont display. When I do it without putting it in a function, I can get the image, but not within the function.

    I've used get_post_meta($post->ID, 'banner-image', true) and set it to a variable. Then I try to call that variable, but nothing comes out. I've been working on this for weeks, and nothing. I've looked on the internet, and still nothing. I don't know if I just don't know what I'm doing and missing something, or if it just can't be done. I hope that someone can give me some ideas to work with. (BTW: this is a wordpress site)

     <?php 
    function retry($catposts) {
      query_posts($catposts);
        if ( have_posts() ) : while ( have_posts() ) : the_post();
            echo '<div id="new-lectures" class="post">';
              echo '<//h2><//a href="'; the_permalink(); 
              echo '">'; the_title(); 
              echo '<///a><///h2>';
              
              $banner = get_post_meta($post->ID, 'banner-image');
              echo '<//img src="' . bloginfo('template_directory') . $banner .  '"/>'; 
              
              echo '<//div class="content">';
              echo the_content(); 
              echo '<///div>'; 
              echo '<///div>'; 
        endwhile; 
      wp_reset_query();
    endif;
    }
    ?>
    

    Thanks Doc for the help with getting the code to show up, but I had to use a TON of backslash marks to get html tags to show up properly. I probably just dont know what Im doing. But, either way, this is the code Im using, minus all the backslashes (except when needed to close a tag, of course).

  • To learn about how to enter code, read this: http://daringfireball.net/projects/markdown/syntax

    I've added some backticks (`) to your first post to show you what happens. If you want to show blocks of code, just indent four spaces.

      // like this
    
  • since you already used query_posts, you can also pass along array('tag'=>'tag-slug-name')