• I’m trying to make a page in a WordPress installation that shows the page’s content, then lists a few featured posts (in a “Featured” category) at the bottom. Each of the featured posts should have an image to go with it, defined by a custom field.

    I took my page.php and added this at the bottom:

    `<?php
    global $post;
    $myposts = get_posts('numberposts=2&category=26');
    foreach($myposts as $post):
    setup_postdata($post);
    ?>
    <div class="feat">
    <?php getFeatImg(); ?>
    <p>[Post excerpt goes here]</p>
    </div>
    <?php endforeach; ?>`

    The getFeatImg() function does this:

    `global $post;
    $filename = get_post_meta($post->ID, 'feat-img', true);
    if ($filename) {
    echo '<img src="/post-images/' . $filneame . '" alt="' . $filename . '" />'; }`

    What I would expect to happen is the images show up if their filenames are in the feat-image custom field, and if the field isn’t set, no images show up at all. What happens instead is that images don’t show up if the field isn’t set, but if the field is set, I get broken images – evidently $filename is set enough to trigger the img tag, but it’s not actually appending the content of the feat-img field to the path, so all the images point to the directory /post-images/. Can anyone tell me what’s going on here?

Viewing 1 replies (of 1 total)
  • Thread Starter wmhunter

    (@wmhunter)

    Argh! I figured it out rereading the post: I have $filename misspelled in my functions.php file. D’oh!

Viewing 1 replies (of 1 total)

The topic ‘get_posts and custom fields’ is closed to new replies.