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
WordPress design tweak results are in
Did you vote? I did. Did your vote win? Mine didn’t 🙁 Results. Advertisements
Automattic launches talkpress.com
I was just working on re-launching my forum in preparation of the new theme I am going to release sometime...
Acquisition of wp.com
Just announced by Matt himself, that they finally acquired wp.com from Yahoo. Head over to the WordPress.com blog, leave a...
WordCult Theme release 0.3
I’ve had some time the last few days while my XPS computer has been having difficulties and the Dell replacement...
powered by WassUp
Recent Posts: Fully Net Worth . com
Adele Roberts is a DJ and a personal assistant. She is known for being one of the cast members of the third edition of Big Brother in the United Kingdom. She is now one of the contestants to take part in television show “I’m a Celebrity..Get Me Out of Here!”. As of 2019, Adele Roberts […]
Jacqueline Mary Jossa is an English actress who is best known for her role in the character of Lauren Branning in the popular BBC soap opera called EastEnders. She is one of the celebrities participating in the nineteenth series of I’m a Celebrity…Get Me Out of Here!. As of 2019, Jacqueline Jossa net worth is […]
Cliff Parisi is an English actor who is best known for his role as Minty Peterson in the well-known BBC soap opera called EastEnders and BBC period drama Call the Midwife. He is taking part in the nineteenth series of I’m a Celebrity…Get Me Out of Here!. As of 2019, Cliff Parisi net worth is […]