WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get Custom Fields, display for multiple posts (11 posts)

  1. oh_hello
    Member
    Posted 7 years ago #

    Hi all,
    I'm using the Get Custom Field Values plugin to display custom meta data for my posts throughout my site.

    However, it isn't working completely as it is displaying only the meta data of the most recent post for all the posts, instead of displaying each post's individual meta data. The code I am using:

    <?php
    $posts = get_posts('numberposts=3');
    foreach($posts as $post) :
    ?>
    <?php echo c2c_get_recent_custom('date', '<p class="eventdate">', '
    '); ?>
    <p class="event"><?php the_title(); ?>
    <?php echo c2c_get_recent_custom('location', '', ''); ?>
    <?php echo c2c_get_recent_custom('time', '', '
    '); ?>
    <?php endforeach; ?>

    Can anyone see what I am doing wrong and how I could remedy the situation? If it helps at all, the site I am working on is located here.

    Thanks in advance.

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Yeah, your loop is incomplete. Right after the foreach line, add this:
    setup_postdata($post);

  3. oh_hello
    Member
    Posted 7 years ago #

    I have added your code, but it doesn't seem to be making a difference. The portion of the code in question:
    <?php
    $posts = get_posts('numberposts=3');
    foreach($posts as $post) :
    setup_postdata($post);
    ?>

    Did I do something wrong? Is there another issue?

    Thanks.

  4. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Huh. Okay, I have no idea what "c2c_get_recent_custom" actually does then. I thought it was getting custom metadata from a post in the loop, but I guess not. Never mind.

    I would just use the built in functionality of post_custom() myself, instead of using a plugin.

  5. oh_hello
    Member
    Posted 7 years ago #

    I don't think I can acheive what I want with built-in functionality. Each of my posts is an Event, and for each Event I have custom meta data such as date, time, location, etc. So for each post I need to have this custom meta data outputed with it. Is this possible using post_custom() and if so, how could I accomplish it?

  6. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    So for each post I need to have this custom meta data outputed with it.

    Do something like this in your loop:
    <?php
    echo '<p class="eventdate">';
    echo post_custom('date');
    echo '</p><p class="event">';
    the_title();
    echo post_custom('location');
    echo post_custom('time');
    echo '</p>';

    Basically, post_custom() returns the string associated with the given key. But it has to be used in the loop, so it has an associated post's meta data to look at.

  7. oh_hello
    Member
    Posted 7 years ago #

    I really appreciate all your help so far, it's been great. But I still can't seem to get it working. The code I am using now:
    <?php
    $posts = get_posts('numberposts=3');
    foreach($posts as $post) :
    ?>
    <?php
    echo '<p class="eventdate">';
    echo get_post_custom_values($date);
    echo '</p><p class="event">';
    the_title();
    echo post_custom('location');
    echo post_custom('time');
    echo '</p>'; ?>
    <?php endforeach; ?>

    And only the post's title is displaying, none of the costom meta data is displaying at all.

    Have I done something wrong?

    Thanks again for all the help so far.

  8. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Yeah, you still don't have the setup_postdata() call in there. And what is $date?

    Like I said, it would help a *lot* if I knew what exactly you were trying to do. A visual aid or something would be useful.

  9. oh_hello
    Member
    Posted 7 years ago #

    The site I am working on: is right here.
    You'll notice on the homepage, I have "Upcoming Events". Each one of those events is a Post. The name of the event is the post title, e.g., Planned Giving Seminar. I then have custom metadata for the date, location and time.
    What I want is for each post to display its own custom metadata with it.
    Hopefully this is clear enough, and I really appreciate all your help so far. Let me know if there's anything else I need to show you.

  10. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    Seems fairly straightforward.

    <h1>Upcoming Events</h1>
    <?php
    $posts = get_posts('numberposts=3');
    foreach($posts as $post) {
    setup_postdata($post);
    echo '<p class="eventdate">';
    echo post_custom('date');
    echo '</p><p class="event">';
    the_title();
    echo '<br/>'.post_custom('location');
    echo '<br/>'.post_custom('time');
    echo '</p>';
    }
    ?>

    Does this work? I'll explain it so that you can understand what it's supposed to do:

    get_posts() gets your posts and puts them in the $posts array.

    foreach() goes through each entry in that array, sets $post to it, then runs the enclosed code.

    setup_postdata() read the post and sets up the globals necessary for the rest of the stuff to work.

    post_custom() gets the value of the given key in the current post being processed (thus the need for the setup_postdata() call), and returns it. If there's more than one, it returns an array of them, which I didn't handle here. I assumed each post only has one entry for each key.

    the_title() is much the same, it spits out the title of the current post being processed.

  11. oh_hello
    Member
    Posted 7 years ago #

    Works like a charm.

    Thank you very much for all your help.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.