Connect with us

Tips & Tricks

Pulling custom fields from outside the loop

Published

on

In the last post “Creating a custom widget” 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 post with a certain custom field from outside the WordPress loop.

In the last post I used this tag:

<?php include(TEMPLATEPATH . '/includes/showcase.php'); ?>

Now I will show you what the file showcase.php has:

<ul>
<?php
global $wpdb;

$sql = "SELECT wposts.*
	FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
	WHERE wposts.ID = wpostmeta.post_id
	AND wpostmeta.meta_key = 'gallery-url'
	AND wposts.post_status = 'publish'
	AND wposts.post_type = 'post'
	ORDER BY wposts.post_date DESC LIMIT 8";

$pageposts = $wpdb->get_results($sql, OBJECT);
$output = $pre_HTML;

foreach ($pageposts as $post) : setup_postdata($post);  ?>

<li><a href="<?php echo get_post_meta($post->ID, "gallery-url", $single = true); ?>" title="Link to <?php the_title(); ?>">
<img src="<?php echo get_post_meta($post->ID, "image", $single = true); ?>" alt="<?php the_title(); ?>" /></a></li>

<?php endforeach; ?>

</ul>

That’s it! The most important item you may want to change for your own custom field is the line : AND wpostmeta.meta_key = 'gallery-url'. Where you would change the text in bold to match your own custom field value.

Update:

Check out Austin from PressedWords comment below.

With his great advise I was able to figure out why all my attempts to use the query_post weren’t working. it came down to this line of code: <?php echo get_post_meta($post->ID, "gallery-url", $single = true); ?>, that was what I had in my code, and the $post->ID is the reason my code would not echo or print the custom field’s value. Solution? replace $post->ID with get_the_ID(). HA, so simple.

Continue Reading
4 Comments

4 Comments

  1. Austin

    January 17, 2009 at 1:49 pm

    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. frosty

    January 17, 2009 at 3:03 pm

    Thanks for that, I will give that code a shot. I tried so many variations, and it wouldn’t show the custom fields, just the html code I input.

    😉

    P.S. If that works I will write a post based on your recommended code!

  3. Pingback: Weekend Links - Jan 23, 2009 | OMNINOGGIN

  4. Guennadi M

    June 14, 2009 at 8:56 am

    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

You must be logged in to post a comment Login

Leave a Reply

Tips & Tricks

Adding a external file after the first post

Published

on

How might you display a Google ad after the first post or anything you like? It is very simple. You just need to add the variable $loopcounter in the Loop. If the $loopcounter is less than or equal to 1, then include your option. Check out the code:

<?php if (have_posts()) : while (have_posts()) : the_post(); $loopcounter++; ?>

  // your loop

  <?php if ($loopcounter <= 1) { include (STYLESHEETPATH . '/you-file.php'); } ?>

<?php endwhile; ?>

<?php else : ?>

<?php endif; ?>

Pretty simple huh. Well in the $loopcounter line, you may change the include to point to any file or maybe a custom widget like so:

<?php if ($loopcounter <= 1) { dynamic_sidebar( 'Plus Post' ); } ?>

Or use your code directly in between the { & }.

Continue Reading

Tips & Tricks

A simple way to query posts

Published

on

Here is a simple way to call query_posts with an array of options. For all options you my use visit: WordPress Codex.

<?php $my_query = array('showposts' => 4, 'post__not_in' => $do_not_duplicate); ?>

    <?php query_posts($my_query); ?>
Continue Reading

Tips & Tricks

Adding a favicon to your site

Published

on

Looking to add a favicon to you site?

Inside your WordPress theme’s functions file (functions.php) add the following to inside your PHP code.

/**
 * FAVICON
 * @WPCult.com
 */
function my_favicon() { ?>
	<link rel="shortcut icon" href="<?php echo bloginfo("stylesheet_directory") ?>'/images/favicon.ico" />
<?php }

add_action('wp_head', 'my_favicon');

That’s it, just be sure to upload an icon image or a .gif/.png. Be sure to correct the target location id the file is located somewhere else.

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Buck Henry Net Worth: How Rich was Buck Henry Actually?

Buck Henry Net Worth: How Rich was Buck Henry Actually?

Buck Henry was an American actor, screenwriter, and director best known for his directing the movie Heaven Can Wait. Buck Henry net worth before his death is estimated to be $6 million. Henry was born on December 9, 1930, in New York City to Ruth Taylor and Paul Steinberg Zuckerman. His mother was an actress […]

Dolores Catania Net Worth: How Rich is Dolores Catania Actually?

Dolores Catania Net Worth: How Rich is Dolores Catania Actually?

Dolores Catania is an American Entrepreneur and Reality Television star best known for being a cast member on “The Real Housewives of New Jersey”. As of 2019, Dolores Catania’s net worth is estimated to be $2 million. Catania was born in Patterson, New Jersey, on December 28, 1970. Her parents are of Italian roots. Her […]

Blueface Net Worth: 5 Interesting Facts About the Rapper

Blueface Net Worth: 5 Interesting Facts About the Rapper

Blueface Bleedem, or simply Blueface, is an American rapper from Los Angeles currently signed to Birdman’s Cash Money West label. He rose to fame after releasing single ‘Deadlocs’ on music platform SoundCloud. The song has received more than 2.5 million streams. Here are some of the interesting facts about Blueface. 1. Blueface net worth is […]

Harry Hains Net Worth: How Rich was Jane Badler’s Son Actually?

Harry Hains Net Worth: How Rich was Jane Badler’s Son Actually?

Harry Hains was an Australian actor known to many for playing on American Horror Story. He is also the son of famous actress Jane Badler. Harry Hains net worth before his death is estimated to be $700,000. Harry was born on December 4, 1992, in Melbourne, Australia to Stephen Hains and Jane Badler. He had […]

Trending