WordPress.org

Forums

Problems displaying custom field data (8 posts)

  1. jeffeggleston
    Member
    Posted 3 years ago #

    I've been trying to create a "Featured Posts" widget for my homepage. This would use a category (featured-event) and would display some data from a couple of custom fields (featured_event_info and featured_event_title). I get the loop started fine, but none of the custom field values are showing. Any help would be greatly appreciated. I have some small knowledge of PHP but for the most part I am a designer. Thanks!

    Here's the code I'm using:

    query_posts('category_name=featured-event&&showposts=3');
    			while (have_posts()) : the_post(); ?>
                <div class="featured-events-post">
                    <a title="<?php echo get_post_meta($post->ID, 'featured_event_title', true); ?>" href="<?php the_permalink() ?>" rel="bookmark">
                        <div class="featured-events-title"><?php echo get_post_meta($post->ID, 'featured_event_title', true); ?></div>
                        <div class="featured-events-shortdata"><?php echo get_post_meta($post->ID, 'featured_event_info', true); ?></a>
                </div>
                <?php endwhile; ?>

    And here's a link to my development site:
    http://library.jeffmeggleston.com/

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    You really need to be using get_posts or WP_Query in the widget. query_posts should only be used to modify the main Loop - not create a secondary Loop.

  3. jeffeggleston
    Member
    Posted 3 years ago #

    $the_query = new WP_Query( 'category_name=featured-event' );
                while ( $the_query->have_posts() ) : $the_query->the_post();
                $featured_event_info = get_post_meta($post->ID, "featured_event_info", true);
    			$featured_event_title = get_post_meta($post->ID, "featured_event_title", true); ?>
                <div class="featured-events-post">
                	<a title="<?php echo $featured_event_title; ?>" href="<?php the_permalink() ?>" rel="bookmark">
                        <div class="featured-events-title"><?php echo $featured_event_title; ?>This is where the data should be</div>
                        <div class="featured-events-shortdata"><?php echo $featured_event_info; ?>This is where the info should be</a>
                </div>
                <?php endwhile; ?>

    Ok, so i changed it and I'm still not getting any custom field data.

  4. jeffeggleston
    Member
    Posted 3 years ago #

    This is confusing. I can get all of the title and content data to show but none of the custom field data.

  5. esmi
    Forum Moderator
    Posted 3 years ago #

    <a title="<?php echo $featured_event_title; ?>" href="<?php the_permalink() ?>" rel="bookmark">

    Where's the other end of this link supposed to be?

  6. jeffeggleston
    Member
    Posted 3 years ago #

    $the_query = new WP_Query( 'category_name=featured-event' );
                while ( $the_query->have_posts() ) : $the_query->the_post();
                $featured_event_info = get_post_meta($post->ID, "featured_event_info", true);
    			$featured_event_title = get_post_meta($post->ID, "featured_event_title", true); ?>
                <div class="featured-events-post">
                	<a title='<?php echo $featured_event_title; ?>' href='<?php the_permalink() ?>' rel='bookmark'>
                    <div class="featured-events-title"><?php echo $featured_event_title; ?>This is where the data should be</div>
                    <div class="featured-events-shortdata"><?php echo $featured_event_info; ?>This is where the info should be</div></a>
                </div>
                <?php endwhile; ?>

    I just fixed that.

  7. jeffeggleston
    Member
    Posted 3 years ago #

    Here's an example of teh data that would show in this case:

    name: featured_event_info
    value: Saturday, Aug. 21 - 8:00pm

    name: featured_event_title
    value: Fiddler On The Roof

    Not sure if that has any bearing on the situation. That is how the data is written in the "value" fields in WordPress.

  8. jeffeggleston
    Member
    Posted 3 years ago #

    I apparently needed to type:

    global $post;

    before the rest of my code. Uugghhh... newbishness....

Topic Closed

This topic has been closed to new replies.

About this Topic