I recently had a conversation with someone who had decided to come to Wordpress to take advantage of a plugin or two and wanted to know how to get an existing site into the correct theme structure as required. It took a lengthy email or two to explain but it struck me that lots of people must be asking the same questions over and over. So in this post i shall try to explain how to get a theme together, or rather where to start
So… first, a bit of logic
Logically speaking a standard layout web page can be split into four distinct areas as follows:
- Header
- Footer
- Sidebar(s)
- Content
And as such Wordpress uses different theme files to render the appropriate sections. We need to split our HTML page into those different files. There are, however, lines of code that Wordpress needs you to put into each of these files and so tbe best way todo this without missing anything is to recycle another theme. So… go to your Wordpress install and visit /wp-content/themes/ and…
- Duplicate the directory called ‘default‘
- Open style.css in a text editor and edit the name of the theme and any other information you feel like
- Either remove or replace screenshot.jpg from the theme directory so it looks different in the theme menu
That’s it, you now have a skeleton to work from and it’s time to make it look like your site. Let’s assume you have a simple brochureware site and, therefore, don’t need any blog type pages. Largely these sites have a standard consistent layout on each page (often except the home page but that difference is for another tutorial) and Wordpress uses this consistency to slot in the appropriate information for each page. We do this by…
- Choose a short yet typical page from your site and view the HTML source
- Open 4 separate text files from the new theme directory in your favourite text editor. They are: header.php, footer.php, sidebar.php and page.php
- From the HTML source take the top section of code (usually as far as the content and sidebar code inclusive of a menu assuming you are using a top horizontal bar navigation) and place it into header.php making sure to preserve the obvious dynamic sections of code (anything between <?php and ?> can be assumed to be dynamic)
- Do the same with the footer into footer.php (the section below the content and sidebar that often contains SEO links and copyright notices)
- The remaining block should contain the content and sidebar code… simply cut out the sidebar code into sidebar.php
- Finally split the content code into the bit before the text and the bit after the text (ie: layout code stripping out the text itself). Then paste these sections around the ‘while’ loop within page.php.
- (optional) copy your stylesheet into style.css making sure to preserve the comment at the top of the file as this is used by Wordpress to define the theme and without which will not pick up any of the code.
Step 6 will likely be the most difficult to do as there are three or four important lines of PHP code in there. Firstly the line that reads <?php get_header(); ?> is the code that calls header.php. Likewise <?php get_footer(); ?> and <?php get_sidebar(); ?> call their appropriate namesakes. Make sure that they are in the correct logical positions for the page to render in the right order.
The Loop
The hardest section is what is called ‘the loop’ by Wordpress bods. This is the section of code that shows the post(s) from the database relative to the page you are looking at. The Loop often falls into the following form:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div>
<?php the_content('<p>Read the rest of this page »</p>'); ?>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
<?php endwhile; endif; ?>
You shouldn’t really need to change any of the code itself but you might need to edit the encapsulating HTML to make your styles fit in. For example if your page titles are not in H2 tags then you need to change it to suit etc etc…
Testing it!
This will always be a case of trial and error until you know what you are doing so don’t worry if it all goes pear shaped. That’s what the undo button is for!
To test that your theme is working properly then you need to visit your sites back end at /wp-admin and click the link on the left hand side called ‘appearance‘. The themes page will appear and to activate your theme you simply find it in the list and click ‘activate’. Then view the front end of the site and see what it looks like.
At this stage you might be confused because the home page looks different to the rest of the site. This is because, by default, Wordpress uses the index.php file from your theme directory to generate the home page. To change this visit the settings -> reading page from the left menu and use the dropdown box to choose an appropriate page to be used for the home page.
What’s next?
Ok so your theme looks like it did before you spent hours making the them for the Wordpress site… why on earth are we using Wordpress at all you ask! Well if you were to create a new page for your old site then you would have to duplicate an HTML file, trawl through code (or look at Dreamweaver or another WYSIWYG) to find the sections to replace before uploading to your site and updating the links in the various menus as appropriate. With Wordpress you just add a ‘page‘ using the nice admin system and it’s all done for you.
I always tell my clients to go and have a look at the Wordpress plugins directory for ideas and things to ad to their site. A word of warning though, some plugins conflict with others and too many plugins might slow your site down considerably. Over time you will get used to a core few you know and trust.
Feel free to ask me any questions about Wordpress, PHP or other and hopefully I will be able to help you decide what’s the best direction for your site to take. Expect a lecture about how good Wordpress is though!
SB Author Comments: Author Commenting plugin for Wordpress
April 15th, 2010To my knowledge it’s not possible to comment on an author in Wordpress. “Why would you want to do that?” you ask… Well for example a client of mine is running a classifieds site using the Classipress theme. The site uses classifieds as jobs for people to do and the people posting and signing up to the site are tradesmen. Comments, in this case, can be used as a feedback system and therefore are a valuable addition.
The wp_comments table doesn’t have support or scope to store comments with an associated user (as opposed to post). At this juncture I would have inserted a null (or 0) value into the post_id column and added a row into the wp_commentmeta table with the user ID it refers to. However, I didn’t write the core code for the project so I simply updated a neat little system written by Dan Wellman. Dan’s system is pretty good and uses AJAX and JSON to add comments to a new database table with two fields (author and comment). It was what I would call ‘a good start’ as DB details were required in a number of different files and the database had room for improvement. I have a few more plans for the code but for now have changed a couple of things and bundled it into a Wordpress plugin for you.
In my version the database table is created for you, it has several more fields including a primary key (!), timestamps and room for additional information (not currently utilised) to be stored. The Wordpress database class is used now so no duplication of the connection details is necessary.
Take a look at the plugin and comments and suggestions for additions are very welcome.
Download
SB Author Comments (1.84 KB)
Usage
Upload and activate the plugin as normal. Then add the following code to your author.php theme template making sure to edit the $author_id variable as appropriate.
<?php if (function_exists('sb_ac_comment_form')) { echo sb_ac_comment_form($author_id); } ?>Credit
Once again credit goes to Dan Wellman who detailed the integration into his site on his blog
No comments »
Posted in AJAX, PHP, Plugins, Time Savers, Tutorials, Wordpress
Tags: comment author Plugin Wordpress Wordpress Plugins