I think I mentioned in last weeks ‘Weekly Good Stuff’ that I was having fun playing around with wordpress at work, and finding out just how powerful this blogging platform is… Well, I figured it would be good samaritan like of me to share the stuff I’ve been figuring out. Here’s the first tastey tidbit.
Pulling in meta/custom fields from other pages.
I needed to pull in some contact details for the sidebar on one page that already exsisted as custom fields (meta) on another page. I’ve been using the flutter plugin, but that’s basically just a fancy way to display custom fields in the admin (making it easier for end users, especially if you’re developing for people who ain’t so savvy). So, I needed to get this data from the entries on one page (contact) to the sidebar on another (about-us). After some googling and searching the WP forums, I found some code that kind of did the right thing. It references the actual database, and the post ID, so it’s not very versatile. However if you’re figuring the post ID of the page you need to reference isn’t gonna change, then you’re in business.
<?
$pages = $wpdb->get_results(”
SELECT * from $wpdb->posts
WHERE post_type=’page’
AND ID= ‘67′
ORDER BY post_title
“);
//the above checks the database for posts that are actually pages, it then (I think?) finds the ID //of the post you need by number. The order by probably doesn’t need to be there anymore, //but I left it in just incase!foreach($pages as $page) :
$address_line_1 = get_post_meta($page->ID, ‘address_line_1′, $single = true);
$address_line_2 = get_post_meta($page->ID, ‘address_line_2′, $single = true);
$city = get_post_meta($page->ID, ‘city’, $single = true);
$postcode = get_post_meta($page->ID, ‘postcode’, $single = true);
$telephone = get_post_meta($page->ID, ‘telephone’, $single = true);
$fax = get_post_meta($page->ID, ‘fax’, $single = true);
$email = get_post_meta($page->ID, ‘email’, $single = true);
// this bit is to pull any meta from the child page
//in this case it’s the custom fields that are used to display the address/contact details.
//echo ‘<h2><a href=”‘ . get_permalink($page->ID) . ‘” title=”‘ . $page->post_title . ‘”>’ . //$page->post_title . ‘</a></h2>’; //this would, if uncommented, show the page title.// right, here I’m echoing the variables that exist in the page we’ve referenced (67 ie. contact)
echo ‘<li>’ . $address_line_1 . ‘ </a></li>’;
echo ‘<li>’ . $address_line_2 . ‘ </a></li>’;
echo ‘<li>’ . $city . ‘ </a></li>’;
echo ‘<li>’ . $postcode . ‘ </a></li>’;
echo ‘<li>Tel: ‘ . $telephone . ‘ </a></li>’;
echo ‘<li> Fax: ‘ . $fax . ‘ </a></li>’;
echo ‘<li>’ . $email . ‘ </a></li>’;
endforeach;
?>
There we go… now I’m no php developer, so this is probably really hacky/shit code… but it works hah. So, I figure if you need to use it, you can. Also, this is why hiphop ain’t so good, I spend my time finding out shit for other sites! Bah. Soon, soon.




