mark.watero.us

Wordpress stuff, a statistics plugin, and jello

Articles found for the word ‘posts’

get_post_ID(), a function for finding yourself

2 comments

Following up on my post about the lack of an is_child() function in Wordpress, I wrote another simple function that I find myself wanting on a regular basis.

Almost every function that pulls information from the database about a specific post, category, or otherwise requires a numerical ID. Well, if you’re like me, and you develop a lot of client sites locally and on your own sandbox server before delivering it to the client, it’s a real pain in the arse to have to go through your code and change ID’s to make them current with the final product.

How come Wordpress doesn’t have get_post_ID()?

13 lines is all, and you can find any posts ID so long as you know what the slug is. This can be used in combination with any number of Wordpress functions to simplify a lot of calls. The only pitfall is that if you change a post/page slug your code will break, but in the case of pages, I’ve found this to be a rare occurrence anyways.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function get_post_ID ($the_name = '') {
	global $wpdb;
 
	if ( empty($the_name) )
		return FALSE;
 
	$the = $wpdb->get_results( "SELECT ID FROM `{$wpdb->posts}` WHERE post_name = '{$the_name}'", ARRAY_A );
 
	if ( isset($the[0]['ID']) && ! empty($the[0]['ID']) )
		return $the[0]['ID'];
 
	return FALSE;
 
}

Now that you can find the ID of your post, you can do whatever you want with it. For example if you want to link to a specific page somewhere in your template without having to call wp_list_pages(), you have easy access to the permalink via;

1
2
3
4
5
6
$the_id = get_post_ID('about');
echo '<a href="' . get_permalink($the_id) . '">About Us</a>';
 
// or if you prefer nested functions
 
echo get_permalink( get_post_ID('about') );

It’s not too hard to modify this to work with post titles instead of just post names (slugs), but since two pages/posts can have exactly the same title, but can never have the same slug, it’s safer to work with it the way it is.

Written by mark

September 23rd, 2009 at 5:24 pm

Posted in Code Snippets

Tagged with , ,

is_child(), a function for finding your parent

leave a comment

Working on my pet project, I came across a conditional that was very conspicuously missing from the Wordpress core template functions. Where has my is_child() been misplaced?

Building web sites using Wordpress, you’ve probably more than once found yourself using various conditional tags to determine whether you’re on a page, a category, a blog entry, or something else. You can specify exactly what page you’re looking for with is_page(), whether you’re on the front page with is_home(), or whether you’re visiting an archive page with is_archive(). But what if you’d like to know who your parent was, or if you even had one?

I know who my parents are, they were introduced to me by the doctor, and over the years we eventually got to a point where we could remember each others names. It’s a little trickier to know who your parent is in Wordpress, though altogether not very difficult. You can check your $post object to see if you have a parent, and run get_post() if you’d like to know your parents name.

But what if you need to check this often?

Instead of writing the same few checks over and over again on various pages or in different functions, it would be nice if Wordpress had an is_child() function as part of the core. So I wrote one, and I believe in laziness, and everyone else’s right to that same laziness.

My code highlighter has gone on vacation, and I haven’t got the time right now to call in a substitute… sorry! Seems to be working again.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function is_child ($of_page = NULL) {
	global $post;
 
	if (! $post->post_parent || ! is_page())
		return FALSE;
 
	if (! isset($of_page))
		return TRUE;
	else if ($of_page == $post->post_parent)
		return TRUE;
	else if ($of_page == get_post($post->post_parent)->post_name)
		return TRUE;
 
	return FALSE;
}

Add this as a plugin, or in your functions.php, and you can use it in the same way you would use is_page();

1
2
3
if ( is_child() )  // returns true if the page is a child of any 
if ( is_child(12) )  // returns true if the parent ID is 12
if ( is_child('about') )  // returns true if the page slug of the parent is 'about'

Enjoy!

Written by mark

September 22nd, 2009 at 9:40 pm

Posted in Code Snippets, General

Tagged with , ,