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
Search Unleased: A custom WordPress plugin
Most all WordPress theme’s use a a simple search form to search your site. But what it you wanted to...
Limit the characters that display on the_title
Ever wanted to display the title of a post somewhere but limit the amount of characters that are shown? For...
The launch of WordPress.tv
Hey, in case you are unaware, WordPress has launched a new site called WordPress.tv. Check out the full post at...
powered by WassUp
Recent Posts: Fully Net Worth . com
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 […]
Gabby Morrison is a young American social media star who is best known for creating videos on the popular and trendy platform TikTok. In a short amount of time, Morrison has been able to garner over 2.1 million followers on the platform where she has received close to 70 million likes for her videos already. […]
Tony Lopez is an American Tik Tok star who is best known for his skills as a dancer and along with his older brother, Ondreaz Lopez completes the synergy of The Lopez Brothers. He has over 2.4 million followers on his profile on Tik Tok where he has received more than 40 million likes so […]