4 ms·
The excellent Automattic-published starter theme Underscores [1] has been around for over a year now, and it's precursor, Toolbox [2], has been around since at
by jwarren 13y ago
The excellent Automattic-published starter theme Underscores [1] has been around for over a year now, and it's precursor, Toolbox [2], has been around since at least mid-2010. Both were designed by the core developers to be open-source bases for other themes, and indeed have been the base for all the official themes for three years now. There are also other base themes like Bones [3] and Roots [4] which are very popular, but are too heavy for my liking.
[1]: http://underscores.me/ http://underscores.me/
[2]: http://wordpress.org/extend/themes/toolbox http://wordpress.org/extend/themes/toolbox
[3]: http://themble.com/bones/ http://themble.com/bones/
[4]: http://www.rootstheme.com/ http://www.rootstheme.com/
Also, I'm getting nitpicky here, but you also don't exactly call a function to start a loop. You call a function to fetch the posts in an object. You then use a pretty bog-standard while loop to loop through that object. You do call a method of said post object to determine whether or not the loop should continue - is that considered bad practise generally?
I understand why a lot of people think the WP loop is badly constructed. The start of a typical loop in WP is this:
if (have_posts()) : while (have_posts()) : the_post();
which is essentially shorthand for this:
if (have_posts()) {
while (have_posts()) {
the_post();
}
}
I think the cause for some misgivings is that people might not realised that have_posts() is actually doing this:
return $wp_query->have_posts();
So you're actually just running a method on the global post object. You'll make similar post objects for custom queries, and interact with them in an identical fashion.
- kijin 13y agoThe experience I related above took place around 2008/9. Sorry if that's a bit out of date. I looked through your links and it does look like there have been recent efforts to produce bare-bones themes, though the OP's theme is much better documented. What I really want to see is a version of the Codex that actually guides the user through an exemplary theme, and OP's theme comes pretty close to that. And yes, I'm referring to the weird `if (have_posts()): while (have_posts()): the_post();` boilerplate. Back then, for a PHP newbie like me, it looked like a magical one-liner ... until I realized that the loop actually terminates hundreds of lines later with an endwhile/endif statement. It's so easy for newbies to miss the significance of the two colons, especially if they're not familiar with the "alternate syntax for control structures". Although I now understand how the loop works, it still pisses me off that I have to call a function to populate a bunch of hidden variables/objects that are later fetched by other functions. Too much going on behind the scenes == difficult to grok. A simple foreach() loop would have been much more intuitive. Perhaps this will happen sometime soon using the Iterator interface, now that WordPress has finally left PHP 4 behind.
- jwarren 13y agoThere were a few others that I think were around in 2008, like Starkers, but my memory's a bit fuzzy so don't take that with any great authority. I definitely agree that a best-practise step-by-step theme construction guide in the Codex would be very useful. I still see many themes using things like query_posts() instead of creating a new WP_Query or a hooking into the pre_get_posts filter. As I mentioned below, Wordpress would ideally have its built-in functions trimmed dramatically, though this would really murder backwards compatibility. I don't really think it's Wordpress's fault that the Loop uses the alternative PHP control structure, but I agree that the Loop's Codex page is abominable. You may or may not be aware that the Codex is a wiki, and thus poor quality information can and will be added. I would like to see a loop using the Iterator class. However, there are quite a few complexities to that, as mentioned in this feature request [1]. They also point out that you can foreach and count on $wp_query->posts, so you can get to your information in a more obvious PHP-friendly way for now. [1]:http://core.trac.wordpress.org/ticket/20297 http://core.trac.wordpress.org/ticket/20297
- vec 13y agoThe reason that many people think the WP loop is badly constructed is because it entirely works by side effects. A (very simple) WP loop looks like this: <?php while (have_posts()) : the_post() ?> <h2><?php the_title() ?></h2> <?php the_content() ?> <?php endwhile ?> There are four unique functions there. All of them depend on non-obvious global state and three of them are used specifically to produce side effects. the_post() invisibly overwrites a global and changes an iterator. It not only mutates global state, but it does something different if you accidentally call it twice. And WP's the_whatever() functions echo something and return nothing?! Want to do some filtering on the title before you print it? Better use get_the_whatever() instead. WTF?! Heres how it should work: <?php foreach ($post in $wpdb->get_posts() : ?> <h2><?= $post->title ?></h2> <?= $post->content ?> <?php endforeach ?>
- jwarren 13y agoYou're completely right on that, and I couldn't agree more; the mystery of the global states is frustrating to a new WP developer. Additionally, Wordpress has tons and tons of bad functions. If so many themes weren't dependent on them, I'd advocate for entirely stripping out a good chunk of them. Someone needs to write Wordpress: The Good Parts, or at least a really canonical good practise guide. I guess it could be a good weekend project. For the functions you mention, you're actually not too far off what they really do. the_title() [1] is just slightly formatting get_the_title [2], which is mostly just returning $post->post_title. Unfortunately, skipping the built-in functions means also skipping the slightly obtuse but powerful filter system. However, if you're not interested in the built in WP stuff, then there's no harm in outputting the titles directly. Btw, yes, I realise this is mostly academic in the context we're talking about. [1]: http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/post-template.php#L71 http://core.trac.wordpress.org/browser/tags/3.5.1/wp-include... [2]: http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/post-template.php#L102 http://core.trac.wordpress.org/browser/tags/3.5.1/wp-include...
- sopooneo 13y agoWhy do you need the if? Shouldn't the while loop run zero times if have_posts() returns NULL or an empty array?