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

Tips & Tricks

Remove the title attribute using jQuery

Published

on

In WordPress, when you use wp_page_menu your anchor attribute’s usually carry a title with the same name. I’m not sure if it’s correct to do this, but it bother me when I hover over a page menu navigation link and I get a hover of the title.

So on my site I used jQuery to remove the title:

$("#nav a").removeAttr("title");

Pretty simple huh?

Continue Reading

Tips & Tricks

How to: Open external links in a new window

Published

on

Over the weekend, I attended WordCamp Denver, and I was asked by John Hawkins how to force links to open in a new tab with out editing the source code. So, today lets learn a simple jQuery trick to open all external links in your site in a new tab or window. We are going to make sure you have jQuery active on your site, you can do this easily in WordPress, since it’s bundled with the latest installations. Use this code in your header: <?php wp_enqueue_script('jquery'); ?> then, below the wp_head add the following:

<script type="text/javacript">
var $j = jQuery.noConflict();

$j(document).ready(function() {
//external attribute
    $j("a:not([@href*=http://YOURSITE.com/])").not("[href^=#]")
        .addClass("external")
        .attr({ target: "_blank" });
    }
);
</script>

That’s it, just make sure you change the http://YOURSITE.com to your website.

Update

If you like you can remove the var $j = and replace all $j with simply just $

 

Continue Reading

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

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Joey Chestnut Net Worth: How Rich is the Competitive Eater Actually?

Joey Chestnut Net Worth: How Rich is the Competitive Eater Actually?

Joseph Christian Chestnut is an American competitive eater. But, he isn’t just any competitive eater, he is currently ranked as number one in this unique sport according to Major League Eating. As of 2019, Joey Chestnut net worth is estimated to be $2 million. Chestnut was born on November 25, 1983, in Vallejo, California. Having grown […]

Shroud Net Worth: How Rich is Streamer Mike Grzesiek Actually?

Shroud Net Worth: How Rich is Streamer Mike Grzesiek Actually?

Michael Grzesiek, who is better known as Shroud, is a video game streamer and a former pro-Counter-Strike: Global Offensive (CS:GO) player. He was one of the most popular streamers on the live streaming platform Twitch but recently made a bold move to join Microsoft owned new streaming platform named Mixer. As of 2019, Shroud net […]

Lil Mosey Net Worth: How Much is the Rapper Worth in 2019?

Lil Mosey Net Worth: How Much is the Rapper Worth in 2019?

Lathan Moses Echols, who is better known by his stage name as Lil Mosey is an American rapper and songwriter. He is best known for his hit song called “Noticed” that managed to peak at the number 80 in the Billboard Hot 100 charts. In 2019, Lil Mosey was included in the XXL Freshman Class […]

Marie Yovanovitch Net Worth: How Rich is Marie Yovanovitch Actually?

Marie Yovanovitch Net Worth: How Rich is Marie Yovanovitch Actually?

Marie Yovanovitch is an American diplomat who served in a variety of positions at the State Department. She worked as the Ambassador to Ukraine from 2016 to 2019. As of Marie Yovanovitch net worth is estimated to be $2 million based on her government salary only. Yovanovitch was born in 1958 in Quebec, Canada to […]

Trending