Practical theme design, Part 1

datePosted on 14:05, July 7th, 2008 by MillaN

All over the internet you can find tutorials, help, samples on creating wordpress theme. Still, there are some topics that are not covered by many of them. I will try to focus on the solution of practical problems I had while creating Starscape theme.

This first part will cover following problems and solutions:

  1. Using theme Wordpress options within CSS or JavaScript file
  2. Adding Gravatar support to the theme
  3. Separating trackback from comments
  4. Creating Post Date button

And as always, Starscape theme contains all this, so you can check it out also.

Using theme Wordpress options within CSS or JavaScript file

If you need to have some dinamyc settings in CSS or JavaScript, you can use PHP to modify styles or JS code. But you might also need to load Wordpress options and use them. Both CSS and JS are external resources and they can’t communicate with Wordpress code directly.

This is actually quite simple to accomplish. You need to created css or js file, but it has to have extension php, like this: ‘mycode.js.php’ or ‘mystyles.css.php’. On the top of this file add following code:

7876cda9506b02934d9d87ec31b67ca1000

First part of the code will go through paths backward from the folder where the file is located, until it founds Wordpress config file. This file will import Wordpress into CSS or JS file.

Header line is there to ensure that created file after going through PHP is of the right type:

  • for CSS: header(’Content-Type: text/css’);
  • for JS: header(’Content-Type: application/javascript’);

Now you have complete Wordpress classes and functions in your CSS or JS file as you would have them in any other PHP file in Wordpress.

Adding Gravatar support to the theme

Wordpress from version 2.5 has few built in functions to help you getting gravatar icons. But you need to implement how this will be used by the theme. WP older then 2.5 has no gravatar support. To get gravatar image you only need email address. You can also specify few additional options. More on these options you can find on official Gravatar website: http://en.gravatar.com/site/implement.

Here is the method you can use to get a gravatar image regardless of Wordpress version you use. This code will generate image URL for email:

7876cda9506b02934d9d87ec31b67ca1001

Default image is the image that will be shown if the gravatar for the email is not found on the gravatar website. Adjust this default image url based on the image you want to use.

Separating trackback from comments

Wordpress puts trackbacks or pingbacks into the comments table in the database, and on the regular wordpress page comments and trackbacks are displayed together. If you like to separate these two, you need to make few changes to the ‘comments.php’ (typical file for comments). If you look at the contents of this file, you will see a foreach loop: foreach ($comments as $comment) {…}. $comments is a Wordpress array with comments for the post. Each $comment is an object with actual comment contents. To separate comments and trackbacks you only need to check comment_type property of the $comment. If the property is empty, then it’s a regular comment. If the value is ‘pingback’, then you are dealing with trackback/pingback.

Easiest solution is to duplicated foreach loop, and in one instance to check if the comment is pingback, and display only them, and then to repeat the loop, and check if the comment is not pingback and display only them. More complex solution can be found in my Starscape theme.

Creating Post Date button

For this you will need example image. You can save and use the one on the rig ht. Positioning of the month and date on this button is done with few CSS classes. You also need to add some html/php code into the Wordpress loop where the post/page is displayed. Post loops are ususally found in ‘index.php’, ’single.php’. Loop for page is in ‘page.php’. HTML/PHP code for the button looks like this:

7876cda9506b02934d9d87ec31b67ca1002

As you can see, we use 3 CSS classes. In postdate class you need to put url to your image. Float will determine if the button will be displayed on the left or the right. Depending on where you plan to use it, you might not need float. This CSS is based on the button for the Starscape theme. Here is the CSS code:

7876cda9506b02934d9d87ec31b67ca1003

Conclusion

This is the first article with few tips that you might need for your own wordpress theme. If you have any suggestions on what you would like to see in the next part(s), please leave the comment or send me an email.

Rating: 7.0/10 (3 votes cast)
  • Technorati
  • StumbleUpon
  • TwitThis
  • del.icio.us
  • Google
  • Facebook
  • Sphinn
  • Mixx
  • Taggly
  • Socialogs
  • YahooMyWeb
  • Yigg
  • E-mail this story to a friend!
  • co.mments
  • Blogosphere News
  • LinkedIn
categoryPosted in Design, Tutorials | printPrint

Comments:

jbj on July 7th, 2008 at 3:41 pm
Gravatar for 'jbj

Great post! I did something similar in French on one of my blogs.

Rating: 0.0/5 (0 votes cast)

Chaoskaizer on July 10th, 2008 at 9:13 am
Gravatar for 'Chaoskaizer

*thumbs up* good articles, the wp-config solution is useful for back compat.

Rating: 0.0/5 (0 votes cast)

Webdesign Info on July 31st, 2008 at 5:38 pm
Gravatar for 'Webdesign Info

Thanks for the Post. It was great to read and great info.

Rating: 0.0/5 (0 votes cast)

Web Design on August 30th, 2008 at 1:45 pm
Gravatar for 'Web Design

Definite bookmark! Will be coming back for more.

Rating: 5.0/5 (1 vote cast)

Prochorus on November 6th, 2008 at 6:01 am
Gravatar for 'Prochorus

I recommend to subscribe to RSS place in a conspicuous place! Readers will be more! Especially at a blog, how are you! Tested - a 30% increased the number of subscribers!

Rating: 0.0/5 (0 votes cast)

bugjee on November 15th, 2008 at 2:48 pm
Gravatar for 'bugjee

great post no body write this without good knowledge

thanks

Rating: 0.0/5 (0 votes cast)

Trackbacks:

Post a Comment

Name:
Email:
Website:
Rating:
Comments: