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
<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.
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
get_the_ID(). HA, so simple.
Random Search Terms
Steve Souders, speeding up themes [notes]
Steve Souders, who currently works at Google is here to talk about speeding up your themes. Some overview notes WhySlow...
The state of the Press
I might like to start off by saying Happy Birthday WordPress. I don’t have any notes from Matt’s presentation, just...
Podcasting with Dave Moyer
Planning Who let you in? Format Daily Weekly Monthly Hour/half hour A show’s Outline – Intro -What is this thing?...
Cooking with BuddyPress
I’m sitting downstairs at WordCamp 2009 in San Fransisco. Up on stage right now, Andy Peatling creator of BuddyPress. BuddyPress...
powered by WassUp
Recent Posts: Fully Net Worth . com
Amalaratna Zandile Dlamini, who is better known by her stage name as Doja Cat, is an American rapper, songwriter, and record producer. She is best known for her hit song called “Mooo!” which was released in 2018. Recently, she released her second album called Hot Pink. As of 2019, Doja Cat net worth is estimated […]
Passion Jonesz is an American YouTube star. She is best known for largely focusing on clothes and cosmetics-related content on her channel. As of 2019, Passion Jonesz net worth is estimated to be $400,000. Jonesz was born on March 12, 1992, in the United States. Very limited information is available on her including her early […]
Jadah Blue is a reality television personality who is best known for her time on the VH1 reality show called Black Ink Crew which showcases the everyday operations and activities of the staff members at the tattoo shop. She was a part of the crew for season 6 &7. As of 2019, Jadah Blue net […]