WordPress: Sticky Posts On Static Front Page

I built my Dad a site, and while he still emails me the content, I still want the code to be easy to use for me and, in theory, for him one day.  The original design was (and is) a static front page with the whole company spiel.  Dad rarely changed things since 1999 anyway, so I figured I was pretty safe keeping that layout.  I did add in a ‘News’ page, which he could use to ‘blog.’  I keep trying to explain that it’s not being a nerdy blogger, it’s a way to reach out to his readers, but … well, it’s a paradigm shift for him.

Then February happened.

Dad lives in Japan, you see, and among other things, works on Risk Analysis for nuclear power plants.  This Fukushima disaster is his bread and butter and in his backyard.  Obviously he’s involved.  This had a round-about way to making he delve into theming more than I normally do, because my Dad wanted to put something on the front of his page.  I thought that it would be a one time thing, but last night he wanted to change it with a new thing.  Suddenly Dad needs featured posts!

On one of my sites, I use a special category for featured posts, list five in a sort of news/image scroller that swipes across your screen.  It works.  What Dad needed was a little different:

  1. A way to flag a post as ‘featured’ or ‘forward facing’
  2. Keep his main ‘page’ stuff below
  3. Show videos (embeds don’t work in the_excerpt)
  4. Show nothing if you have no featured posts

Thanks to Twitter, I came up with a pretty simple answer.  For a brief moment, I toyed with making this a widget, but since I already had a special front page template for the site, instead I added a special loop that runs at the top:

 <?php
	$sticky = get_option( 'sticky_posts' );
	rsort( $sticky );
	$sticky = array_slice( $sticky, 0, 1 );

if (is_numeric($sticky[0])) {
	/* Query sticky posts */
	query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
		while ( have_posts() ) : the_post();
		the_title('<h3>', '</h3>');
		if($post->post_excerpt) :
			the_excerpt();
		else:
			the_content();
		endif;
		endwhile; // End the loop. Whew. 

	wp_reset_query();
}
?>

From Justin Tadlock we get the very helpful Get the latest sticky posts in WordPress which I used to show the first (and only) sticky. Obviously change the 1 to the number of stickies you want.

<?php
	$sticky = get_option( 'sticky_posts' );
	rsort( $sticky );
	$sticky = array_slice( $sticky, 0, 1 );
?>

Once we’re in the meat of the query, I wanted to show the full post if there’s no custom excerpt, and then just the excerpt if there is one.  This works around the annoying fact that you can’t embed videos in the excerpt (images and italics, yes, but not videos). Also, I tossed in if (is_numeric($sticky[0])) {} around the whole thing. If there is no sticky post, then $sticky[0] (which is the first item in the array $sticky) isn’t a number, it’s ‘null’ (which means ‘Hey! Nothing here!). My check is simply ‘If there’s any number in here, we’re good!’

<?php
	query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
		while ( have_posts() ) : the_post();
		the_title('<h3>', '</h3>');
		if($post->post_excerpt) :
			the_excerpt();
		else:
			the_content();
		endif;
		endwhile; // End the loop. Whew. 
?>

At the end I called in wp_reset_query(); so I could reset the query and go back to the regularly scheduled post.

There’s really not a whole lot customized on Woody.com, but what there is works and it’s simple. I try to keep everything such that if Dad every decides he can do this stuff, it will be straightforward so he can do it. Telling him ‘Mark a post as sticky and it’ll show on your home page’ is easy. He can do that.

About these ads
StudioPress Theme of the Month
Half-Elf? Try Half OFF WordPress ebooks!