Tag: wordpress (page 2 of 2)

Add first and last classes to your loop without using JavaScript

So you’re a pixel-perfect designer who wants to keep control over your WordPress loop styles? Hell yeah! You’re already pretty cool in my book.

You’ve probably got a fancy post separator, or a brilliant doodle to fit between your last post and the comments. Whatever the reason, you don’t have CSS class selectors for targeting the first or last posts in your archive – and you really need them.

There are a lot of tutorials on how to achieve this with jQuery. But it’s not worth relying on JavaScript for something that can easily be done with a little PHP magic.

First, insert this function into your functions.php file.

Now, open up loop.php and replace post_class() with the newly created fjarrett_post_class().

This new function accepts the same parameters as the original function, so you can use it the exact same way. The only difference will be that the first and last posts will be marked automatically with an appropriate class name. Enjoy total control. 🙂

If this helped you in any way I’d love to hear about it in the comments!

The simplest way to require/include wp-load.php

If you want to use WordPress functionality in a PHP file that exists outside of your WordPress installation then you need to include wp-load.php. Perhaps you could call this “hooking into WordPress”.

Maybe you’re already using some sort of relative path method, like:

<?php include '../../../wp-load.php'; ?>

But this can create problems if directories change. You need a clean, dynamic way to get wp-load.php. So here is the simplest way to do it, with just two lines of code (place it at the very top of your file):

1
2
3
4
5
6
<?php
 
$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );
 
?>

Short and sweet 🙂

Disclaimer: This is intended for experimental and development purposes only. It is not advised to redundantly load WordPress on live production environments. But, why?

Joining X-Team

Not long ago I was contacted by Dave Rosen, the CEO of X-Team, who had stumbled across my blog (this one). He was looking for a WordPress guru who could help wrangle up their many projects on the technical/development side of things.

After just three days of communicating back and forth (and one Skype call) he offered me a full-time position, and I wholeheartedly accepted! He flew me to Los Angeles a few days later to sync up with his top developers, Weston and Josh, who have been temporarily living here while working at FOX Broadcasting.

Needless to say, it’s been an outstanding experience working with these two. They’re not only top notch programmers but also great guys who are a blast to hang out with and have become good friends of mine. Since I’ve been here I’ve really enjoyed soaking up all the new information and techniques X-Team uses, specifically learning to incorporate version control with Git via command line into my workflow. I’m very happy to say I’m no longer Cowboy Coding! 🙂

My next big task (apart from client projects) is to bring a WordPress framework into fruition for X-Team that we’re calling: WPized. Weston Ruter has laid a ton of groundwork for the WPized framework already, so I’ll be taking a lot of what he’s started and simplifying it into a tool for creating themes rapidly. I’ll also be writing a lot of documentation for our other WP developers so they will know how to use the custom helper functions the framework will offer and all of this will hopefully make the process of theming much more unified within X-Team.

It’s an extreme privilege to be doing what I love for a company that boasts some very big clients, has a long history of doing extraordinary things and has a lot of fun doing it! I am very fortunate to not only be running my own theme shop but now also working with a superb international team on WordPress projects full-time…from home!

Create a dropdown of custom taxonomies in WordPress (the easy way)

So you’ve been busy taking advantage of custom post type functionalities in WordPress since mid 2010. And of course you’re using custom taxonomies too, right? Of course you are.

If you’re a theme or plugin developer you may have ran across the need to populate a dropdown list of your custom taxonomies. Essentially there are two different (easy) ways to accomplish this. One you always hear about and the other you don’t.

Method #1

Since WP 2.1 the wp_dropdown_categories function has been around but in WP 3.0 the taxonomy argument was introduced. So just calling this function and using the taxonomy argument is probably the absolute easiest way to populate a dropdown list of your custom taxonomies.

<?php wp_dropdown_categories( 'taxonomy=my_custom_taxonomy' ); ?>

This method is great if you need the output of your dropdown values to be the category ID. Because this is the HTML that will be generated:

1
2
3
4
5
6
<select name="cat" class="postform">
	<option value="3">Tax A</option>
	<option value="14">Tax B</option>
	<option value="26">Tax C</option>
	<option value="29">Tax D</option>
</select>

However, let’s say you want your option value output to be the taxonomy’s slug instead of the ID. Well, that’s impossible to achieve using the wp_dropdown_categories function.

Peering into the WordPress core we see that this function is using a walker class called Walker_CategoryDropdown. This walker is designed to output only the ID as the value for each dropdown item. There is not an argument in the function to control value output.

Method #2

That’s where Method #2 comes in. We’ll have to write our own custom function that will generate the dropdown so we can output each option value as a slug:

1
2
3
4
5
6
7
8
9
10
function fjarrett_custom_taxonomy_dropdown( $taxonomy ) {
	$terms = get_terms( $taxonomy );
	if ( $terms ) {
		printf( '<select name="%s" class="postform">', esc_attr( $taxonomy ) );
		foreach ( $terms as $term ) {
			printf( '<option value="%s">%s</option>', esc_attr( $term->slug ), esc_html( $term->name ) );
		}
		print( '</select>' );
	}
}

So, now that we’ve got a cool custom function, we can call anywhere in our code like so:

<?php fjarrett_custom_taxonomy_dropdown( 'my_custom_taxonomy' ); ?>

Expansions on Method #2

If you’re a coding rockstar you can take Method #2 even further by making room for more parameters. This will give you even more control and make it function more like wp_dropdown_categories does:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function fjarrett_custom_taxonomy_dropdown( $taxonomy, $orderby = 'date', $order = 'DESC', $limit = '-1', $name, $show_option_all = null, $show_option_none = null ) {
	$args = array(
		'orderby' => $orderby,
		'order' => $order,
		'number' => $limit,
	);
	$terms = get_terms( $taxonomy, $args );
	$name = ( $name ) ? $name : $taxonomy;
	if ( $terms ) {
		printf( '<select name="%s" class="postform">', esc_attr( $name ) );
		if ( $show_option_all ) {
			printf( '<option value="0">%s</option>', esc_html( $show_option_all ) );
		}
		if ( $show_option_none ) {
			printf( '<option value="-1">%s</option>', esc_html( $show_option_none ) );
		}
		foreach ( $terms as $term ) {
			printf( '<option value="%s">%s</option>', esc_attr( $term->slug ), esc_html( $term->name ) );
		}
		print( '</select>' );
	}
}

Then call it in your code like so:

<?php fjarrett_custom_taxonomy_dropdown( 'my_custom_taxonomy', 'date', 'DESC', '5', 'my_custom_taxonomy', 'Select All', 'Select None' ); ?>

Conclusion

As you can see, WordPress has done a fabulous job of making room for you and I to do pretty much whatever we want.

To reference all available arguments and parameters, please see:
http://codex.wordpress.org/Function_Reference/wp_dropdown_categories
http://codex.wordpress.org/Function_Reference/get_terms

Was this code helpful to you? Let me know in the comments!

What’s the future of the WordPress iOS app?

I don’t launch the WordPress iOS application very often, and when I do it’s usually to do a quick typo fix on a post, page or comment. Sadly, there is no other reason to use it.

Once, I heard Matt Mullenweg mention in a podcast interview (can’t remember where, sorry) that he really wasn’t happy with the app and agreed there was still a lot to be done. I’m always glad to see Matt pushing for more and it makes me very optimistic about the app’s future.

I don’t intend for this post to be a rant, I just believe that in order for the app to have a successful future, it should be supporting what WordPress is already known for. Otherwise, Tumblr, Posterous and a slew of other platforms who are thinking more “mobile” could gain a significant portion of the WordPress market share.

It seems to me that there are five major components of WordPress that just aren’t supported by the app yet.

1. Custom Post Types
This is a big one. WordPress introduced them in June 2010 with version 3.0 and we still don’t have them supported in the iOS app. This is probably the biggest upset of the five since it has been around for over a year and involves the very core of WordPress: Writing posts.

2. Widgets
Seriously? Yes, seriously. Currently, there is no way to add, modify or remove widgets from your WordPress site using the iOS app. Some may disagree that this belongs on my list, but I think it’s deserving. Widgets were introduced in version 2.2 and are one of the core things that makes WordPress unique and attractive. The mobile app should reflect WordPress’s existing core strengths to differentiate itself from other mobile players.

3. Post Formats
Post formats were introduced in version 3.1 and I think it was an excellent addition. It took the Custom Post Types functionality from version 3.0 and expanded it even further. We even saw the birth of “Tumblogs”, or WordPress themes that function like Tumblr sites, all because of Post Formats. Hell, I can’t even use the default Twentyeleven theme effectively on iOS because support for Post Formats is still missing. This would be a huge improvement.

4. Custom Fields (Post Meta)
Some plugins and advanced themes take advantage of Custom Fields on WordPress posts and pages. Although not always crucial, this is another capability that already makes WordPress unique and is part of core functionality so I believe it should be a part of the mobile strategy.

5. Theme Options
Almost every theme these days has a variety of Theme Options. And I’m not just talking about the “Theme Options” item under the Appearance menu, but also Header, Background, Sidebars and any other options page available for a particular theme. Why is this last on my list? Because Theme Options aren’t exactly critical for delivering content, which is what mobile is really all about: creating and publishing content quickly while on-the-go.

Jump on over to the WordPress iOS App Forum to join the conversation or be part of the solution.