The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End get_post_meta() not working within a function

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #39981

    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)

    function retry($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 ‘‘;

    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:

    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’)

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.