Grow your CSS skills. Land your dream job.

get_post_meta() not working within a function

  • # September 23, 2012 at 11:32 pm

    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 ‘

    ‘;
    echo ‘
    echo ‘”>'; the_title();
    echo ‘‘;

    $banner = get_post_meta($post->ID, ‘banner-image’);
    echo ‘‘;

    echo ‘‘;
    echo the_content();
    echo ‘‘;
    echo ‘‘;
    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).

    # September 24, 2012 at 1:53 pm

    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

    # October 9, 2012 at 8:09 am

    since you already used query_posts, you can also pass along array(‘tag’=>’tag-slug-name’)

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".