Do You Have a Unique Loop – Share!
We’ve been understanding how the WordPress Loop works in one of our Lesson discussions, and it’s now time to show off your WordPress Loops.
If you have a unique Loop, would you tell us about it and post the code here? If the code is long, please post it elsewhere and supply the description and the link here.
Come on, show off your Loop!!!
I have enough trouble understanding my Loop, let alone try to explain it! But I can post a snippet or two from it. For example, for posts dealing with plugins, I prefix the post title on home, category and archive pages with the text “Plugin: “. It’s not part of the title or link, just a flag of sorts. What can I say, it amuses me…
Here’s how I do it: First, I select a custom field key called “wp” when composing a plugin post, and add the value “plugin”.
At the start of The Loop (before the title, of course), I have this block of PHP:
$title_prefix = '';
if($keys = get_post_custom_keys()) :
foreach($keys as $key) :
$values = array_map('trim',$post_meta_cache[$id][$key]);
$value = implode($values,', ');
if('plugin' == $value)
$title_prefix = 'WP Plugin:';
Notice I ignore the ‘wp’ key here, as I presently don’t use a value of ‘plugin’ for any other custom field key. If I did, I could have run the check like so:
if(('wp' == $key) && ('plugin' == $value))
Finally, $title_prefix is displayed (thanks to the echo command) with the title:
<h3 class="post-title"><?php echo $title_prefix; ?> <a href="<?php the_permalink(); ?>" title="link: <?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
I’ve shown a specific implementation here, but with a little ingenuity you could put something like this to use for unique images or layout features individualized for one or more type of post.
So in the title of the post article it would say (if the title of the plugin was “Freddy Fuser”, your PHP code would insert the header, based upon what was entered in the custom fields in the Write Post panel at the time you entered the post. So the actual h3 heading would convert to:
<h3 class="post-title">WP Plugin: <a href="freddy-fuser.php" title="link: freddy-fuser">Freddy Fuser</a></h3>
Which would look like (for the simple folk like me):
WP Plugin: Freddy Fuser
That is very cool. Thanks!
[link returns here]
That is so cool! But I am completely ignorant of custom fields and the like. What would you need to put in the custom field value and key to get: “WP Plugin”?
Excellent. We’re still looking for more unique loops all the time, so add them to the list. We’re going to put together a series of examples to help others in the future, and we’re DESPERATE for your loops.
divrom: I’m not sure I understand your confusion (hehe, irony), but I have a plugin in the works which pulls custom field information to do much what I outline above (consider it a generic, plugin version of what I perform in my loop).
If you’d like to play guinea pig, contact me. (email link on my blog’s sidebar)
- The topic ‘Do You Have a Unique Loop – Share!’ is closed to new replies.