This post written by Austin

A web designer and avid WordPress guru. Visit my personal blog, and please follow @WPCult on twitter for site updates. You can also follow me personally @TheFrosty.

  • Share/Bookmark

4 responses to “Pulling custom fields from outside the loop”

  1. Austin

    No need to query the database directly. You can use the WordPress API to accomplish the same thing:

    <ul>
    <?php
    query_posts(array(
    'meta_key' => 'gallery-url',
    'showposts' => 8,
    ));
    while( have_posts() ) : the_post(); ?>

    <li><a href="<?php echo get_post_meta(get_the_ID(), "gallery-url", $single = true); ?>" title="<?php echo attribute_escape(sprintf('Link to %s', get_the_title())); ?>">
    <img src="<?php echo get_post_meta(get_the_ID(), "image", $single = true); ?>" alt="<?php echo attribute_escape(get_the_title()); ?>" /></a></li>

    <?php endwhile; ?>

    </ul>

    If you can, it’s usually better to use the WordPress API, because your code won’t have to change if the underlying database structure in WordPress changes (as it has before), you get the benefits of the built-in object caching (or caching plugins, if they’re being used), and it tends to allow better compatibility with other plugins.

    Austin’s last blog post..WordPress Use Declines Among Top 100 Bloggers

  2. Weekend Links - Jan 23, 2009 | OMNINOGGIN

    [...] Pulling custom fields from outside the loop – In the last post I showed you how to create a custom widget. Well in this post I will show you how I used my custom widget to display all po… [...]

  3. Guennadi M

    To display custom field outside the loop, you need to make the post ID available outside the loop, as per http://www.ausbusiness.net/review/wordpress-custom-fields-outside-loop/

    Guennadi M

Shopping Cart

Your theme cart is empty

External Link Preferences

Feedburner & Twitter Stats

Tag Cloud

Top Commentators

  • No commentators.
  • WordPress Premium Themes

    • Agent Theme
    • Black Canvas
    • Busy Bee
    • CelebrityPress
    • Chrome Theme
    • PaperCut
    • Revolution 2
    • WP Realtor