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

Themes

Fixing plugins not compatible with hybrid 0.4.2

Published

on

Hey everyone, as you may have noticed I have a running on the site. While everything almost everything moved over smoothly, there was an issue with how my parent theme striped text in typography. This caused some plugins, like cformsII to not be allowed to print to the screen. Justin had helped to find a fix to filter the hybrid_typography function.

Originally this function filtered out and changed some text that would not validate as XHTML. This had to be removed by running a new function in the child theme’s function.php file.

add_action('init', 'remove_typography');

function remove_typography() {
	remove_filter('the_content', 'hybrid_typography', 11);
}
Continue Reading

Plugins

Add the twittar plugin for WordPress w/ comment threading

Published

on

I talked about the Twittar plugin in a previous post. But learned that the read me text file was only useful for installing the necessary php code into WordPress themes that don’t have comment threading enabled.

Since most new themes past version 2.7, pull the comments section from functions.php file, that is where you have to edit the avatars in order to show the twittar settings.

First open you functions.php, and look for a line of code that says:

function custom_comment()

This is where your new comments are pulled from when you have the threaded comments enabled.

Now just find <?php echo get_avatar( $comment, $args['avatar_size'] ); ?> & replace with <?php twittar(70, "", "", "avatar avatar-70 photo", 1, "R"); ?>. Keep in mind that you will need to edit the Twittar setting to fit your site.

Continue Reading

Tips & Tricks

Styling your ordered & unordered list items into 2 columns

Published

on

In a previous post I showed you how to create a custom widget, then I showed you how to create a widget for your monthly archives, and limit the month’s that show. I am showing the past 4 months in my widget at this time.

One of my readers: Alex asked:

How did you make the archives widget to show the dates separately into two columns?

Now I will show you a CSS trick to create this effect:

First we will give the unordered list a width of 100%:

ul#archives {
	width:110%;
	}

Then we will style the <li> nested inside.

ul#archives li {
	display:inline;
	float:left;
	width:120px;
	}

And that is it. Please make sure that you clear the float after your closing </ul>.

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Pete Buttigieg Net Worth: 5 Facts You Should Know

Pete Buttigieg Net Worth: 5 Facts You Should Know

Pete Buttigieg is the mayor of South Bend, Indiana. He is also an American war veteran and a former consultant. In the recent days, Buttigieg has been growing his name with him announcing candidacy for the Presidential election in 2020. Then President Obama called him as one of four Democrats who represented the future of […]

Yandy Smith Net Worth

Yandy Smith Net Worth

Yandy Smith Net Worth. The American celeb Yandy Smith has a net value of $15 million. She is a reality TV star and music producer. Yandy started her professional career working in the entertainment industry. She started working as an executive assistant in a company called Violator Management. Some of her clients include high-profile musicians […]

Jasmine Luv Net Worth: How Rich is the YouTube star?

Jasmine Luv Net Worth: How Rich is the YouTube star?

Jasmine Luv is an American Youtuber who is known for her comedic videos which are very relatable to the fans. This has helped her to build a fan following on Instagram of over 1.3 million and is also working on a Youtube Channel that has already crossed 110k subscribers. As of 2019, Jasmine Luv net […]

Jasmine Roth Net Worth: How Rich is Jasmine Roth Actually?

Jasmine Roth Net Worth: How Rich is Jasmine Roth Actually?

Jasmine Roth is the founder of Built Custom Homes which focuses on custom designs for new construction projects. Moreover, Roth is also a television personality being the host of the show called Hidden Potential. As of 2019, Jasmine Roth net worth is estimated to be $1.5 million. Roth grew up in Virginia and since her […]

Trending