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
Content Marketing: 5 Tools to Help Drive Your Instant Results
The ability to make gather the information and properly arrange the content based on the supportive skills. Because of, all...
Why Having Your Site Built In WordPress Is Essential For SEO
WordPress is a highly popular open source content management system that is not only best for blogging, but also for...
What to Consider When Choosing a WordPress Theme for Your Business
If you’ve started looking for themes for your business site, you will understand that there are literally hundreds of thousands...
Data Center Intrusions: No Sign of Improvement
Data Breach Infographic developed by LifeLock.com Advertisements
powered by WassUp
Recent Posts: Fully Net Worth . com
Ryan Garcia is an American professional boxer who competes in the lightweight division. Most recently, he became the winner of the WBO–NABO and WBC Silver lightweight title. He recently signed a deal with Golden Boy Promotions, which is said to be one of the most lucrative boxing deals for a prospect. As of 2019, Ryan […]
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 […]
Kembe X is an American rapper who is best known for his songs such as “Dollar Please”, “Extremely Good”, and “Schemin On The Scoochies”. As of 2019, Kembe X net worth is estimated to be $200,000. Kembe X initially found some success through his debut mixtape called Self Rule but had a dip in the […]