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

Remove spaces when echoing the_title

Ever wanted to print or echo the WordPress title attribute without spaces?

Published

on

This little trick can be useful for calling custom functions and printing the title with out spaces for W3C compatibility. I used this trick in a new theme called Galleria, which will be out for public download in the coming days.

Using this comes in handy for a delicious text link:

<?php $title = get_the_title(); ?>
<a href="http://del.icio.us/post?url=<?php echo $title; ?>&amp;<?php echo str_replace(" ", "%20", $title); ?>">
Bookmark This (<?php echo $title; ?>)</a>

What I am doing is calling $title = get_the_title(); and using str_replace(" ", "%20", $title); to replace empty spaces with a %20, which is used in URL encoding empty spaces.

Alternatively you can use a dash or underscore.

Thanks to Jason Boyle for his adaption.

Continue Reading

Tips & Tricks

Display the_excerpt only if there is text

Have you ever wanted to display the excerpt only if you write one? A simple couple lines of code can display the_excerpt any where you like.

Published

on

In my new theme, I am using this coded trick to display the excerpt on a single post only if I’ve got text inside. Usually if you use the_excerpt and you don’t have one, it will fake one for you.

This is not something that I wanted to do on the single post page. So I used the following code to check if the excerpt existed.

if ( !empty( $post->post_excerpt ) ) :

Once this action is taken into account, you can factor in what code you want to out put if the post_excerpt isn’t empty.

if ( !empty( $post->post_excerpt ) ) :
	the_excerpt();
else :
	false;
endif;

The above code checks if there is an excerpt and print’s it to the screen. If there isn’t an except, it doesn’t do anything.

Continue Reading

Tips & Tricks

Add a shortcode

Published

on

This is a simple one.

/**
 * Your Blog title
 *
 */
function my_blog_title() {
	$blogname = get_bloginfo('name');
    return '<span class="blog-title">' . $blogname . '</span>';
}		  	
add_shortcode('blog-title', 'my_blog_title');

Just add this to your functions.php file and then add [blog-title] in any post or page and it will return your Blog Title. :)

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Danny Towers Net Worth: How Rich is the Puerto Rican Rapper?

Danny Towers Net Worth: How Rich is the Puerto Rican Rapper?

Danny Towers is an upcoming rapper who is best known for his songs “‘Motel Hell”, “Geekin”, and “Trauma”. He is currently on tour with Ski Mask The Slump God and was a good friend to late XXXTentacion. As of 2019, Danny Towers net worth is estimated to be $300,000. Danny was born on April 17, […]

Quin NFN Net Worth: How Much Money Does the Rapper Make?

Quin NFN Net Worth: How Much Money Does the Rapper Make?

Quin NFN is an emerging American rapper who has released songs such as “Talkin’ My Shit” and “Gametime Pt. 2”. He was born on January 18, 2001, in Austin, Texas, United States. As of 2019, Quin NFN net worth is estimated to be $200,000. Though Austin has been the home of some big stars in […]

Travis Hollman Net Worth- How Rich Is Stephanie Hollman’s Husband?

Travis Hollman Net Worth- How Rich Is Stephanie Hollman’s Husband?

Travis Hollman is a Businessman, innovator, and philanthropist. He is currently the President and CEO of Hollman Inc. which is a well-known company working on making sports, fitness, and office lockers. As of 2019, Travis Hollman net worth is estimated to be $20 million. His father had initially founded the company and formed the base […]

Michael Strahan Net Worth.

Michael Strahan Net Worth.

Michael Strahan Net Worth Who does not like that rigorous American Football game that makes your adrenaline take a toll even though you would be just sitting on a chair amongst the spectators?! Everyone does. The mere sight of the game is enough for people to make them sit on the edge of their seats […]

Trending