Ok, I know that I said I wouldn’t really be posting so that I wouldn’t get distracted and so I could focus on my portfolio theme development. But, this is just too cool!
I was at the DFW WordPress May Meetup earlier today (at BJ’s) and met some really smart people that know a whole heck of a lot more than I do (and are much further along “the road” than I am).
One guy in particular, Randy Hoyt, was a great resource for what I’m attempting to do with wordpress. After talking with him for about 5 minutes, we got on the subject of Custom Field’s and he said Custom Field Template was a great tool that would help me achieve my goals.
Needless to say, I left the DFWWP Meetup pretty pumped to get back to my computer to start playing around with all the ideas of what I could do bouncing around in my head!
After I got the CFT plugin installed I decided to take it for a test drive so to speak. (*As I’m new to a lot of this, the nomenclature I use may be off a little, as I continue I hope I do a good enough job describing the process I followed. Also, if there is a better/more effective/efficient process than what I’ve followed, please, enlighten me!)
I made a custom field template that I titled “Portfolio” with the plans of handling all of the ‘extra’ data associated with a portfolio piece (i.e. Client Name, Client Quote, Portfolio Image, Skills used, etc…)
There is a great post by Gabriel Serafini (@gserafini) in which he highlights and does pretty good walk through of setting up your own CFT template. I won’t go into it here, check out his post.
Here we go! I had my template all set up and rigged up some test posts to test out the functionality and work out any bugs. All of the ‘simple’ fields worked beautifully (client_name, portfolio_image, etc…) but I had put in a custom field ‘skills_used’ that utilized checkboxes. Since you can click on more than one box it passes an array to that custom field, which I haven’t had to deal with up to this point. I didn’t know if I could parse the array successfully and apply the html tags I wanted to it as well and place it in the wp template I needed it in.
I found some good posts on the net but nothing that really dealt specifically with this particular issue (granted, I’m sure it is out there, I looked for like 5 minutes max…) so I rolled up my sleeves and went to work.
Here is what I came up with, I’m sure it is pretty sloppy (I consider myself a php novice pretty much.) but it works like a beauty. I decided to wrap everything in an unordered list and wrap each skill used in it’s own list item.
<?php //get skills_used (custom field) ?>
<?php $skills = get_post_meta($post->ID, 'skills_used'); ?>
<?php if(empty($skills)) {
//if $skills is empty DO NOTHING!!!
}
else {
echo '<ul class="skills-used">
<li class="skills-used-title"><h4>Skills Used:</h4></li>';
foreach ($skills as $skill) {
echo '<li class="'."$skill".'">'."$skill</li>";
}
echo "</ul>";
}
?>
*this code was placed into the single template file for my portfolio category posts* (It can go in any wp template you need it to go into, sidebar.php, single.php, etc…)
Description: I passed the custom field ‘skills_used’ which was part for the template that I created in CFT ( Remember the checkboxes? Ya, those guys.) to a variable $skills. Then I tested this variable to see if it was empty, if it was empty I wanted it to do NOTHING, although, if it had the good stuff, I wanted it. But the question was, how to get all of it. Enter “foreach”, basically, the jist of what is happening here is that I’m splitting the array apart and labeling each part it pulls off as the variable $skill (not plural!) echo’s it after wrapping it in the li tag, then forgetting it and moving to the next one, until there is nothing left. Tack on the close ul tag and wrap up the php.
An astute student would notice two ‘$skill’ varables being used each foreach. Very good padawan, I want css control over each one, so I’m listing the class of the list item as $skill. This way, if I use Photoshop CS4 for a particular project, I can place a sexy little blue square with “Ps” carved into it.
That, my friends, is what was so cool that it got me blogging at 1:30 am (pardon my spelling!). There is a whole lot of cool stuff you can do with custom fields and similar implementations to this, it just takes a little imagination and experimentation. Enjoy!