Académique Documents
Professionnel Documents
Culture Documents
To get started with this tutorial we should set up a server on our computer
using either XAMPP or WAMP (usually if working on a PC), or MAMP if you are
working on a Mac. All of these tools allow for a local testing environment for
WordPress.
/images(folder)
style.css
header.php
index.php
single.php
footer.php
archive.php
page.php
sidebar.php
functions.php
Step 1 – Style.css
The style.css will contain all of the elements that style our WordPress theme. We
will use it to declare the name of our theme as well as the author name and link,
and description and version of our theme. Put the following code in the start of
this file, please make sure that it should be a commented code.
/*
Theme Name: New theme
Theme URI: http://www.if-any.com
Description: Anything you want to write here
Author: Your name
Author URI: http://www.if-any.com
Version: 1.0
*/
Now we will create some basic styles in this file so that we will later implement in
some of the theme's files. All IDs and CLASSs are used as we normally write in CSS
file.
body{ #header{
font-family: Arial, Helvetica, Georgia, width: 750px;
Sans-serif; height: 100px;
font-size: 12px; }
background: #d9d9d9;
color: #000000; #blog{
} float: left;
a:link, a:visited{ width: 520px;
text-decoration: none; padding: 0 10px 10px 10px;
color: #000000; }
}
a:hover{ .sidebar{
color: #5f5f5f; float: left;
} width: 200px;
h1 { margin: 0 0 0 10px;
font-size: 54px; font-size: 14px;
} list-style: none;
h3 { }
font-size: 24px;
} #footer{
#wrapper{ clear: both;
margin: 0 auto; text-align: center;
width: 750px; height: 50px;
text-align: left; background: #ccc;
background: #fff; padding: 10px;
padding: 20px; }
}
Step 2 – Header.php
Now we will create the header.php, which at the moment will contain our
website logo, as well as our custom navigation, put the following code inside this
file.
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<title><?php wp_title ( '|', true,'right' ); ?></title>
<?php
/*
* Add this to support sites with sites with threaded comments enabled.
*/
if ( is_singular() && get_option( 'thread_comments' ) )
wp_enqueue_script( 'comment-reply' );
wp_head();
wp_get_archives('type=monthly&format=link');
?>
</head>
<body>
<div id="wrapper">
<div id="header">
<h1><a href="<?php echo get_option('home'); ?>"><?php
bloginfo('name'); ?></a></h1>
</div>
The above code should be in the header.php of all of your themes. First we
declare the doctype, as well as use the standard that will be used to show the
name of your website as you type it in your WordPress settings, and then your
style.css and the PHP code that will enable WordPress 3.0's threaded comments.
Threaded comments?
Now that we have coded up our header.php with our basic information and our
blog's name, we can add our custom navigation menu. Before we actually add the
code to our header.php file though, we have to first open up the functions.php
and add the necessary code to enable the custom menus, because we first have
to enable the navigation then it will be shown in our theme, add the following
code in functions.php file.
<?php
?>
As you can see via the commented sections of the code, the first part, with add
action is used to add support for custom menus, and next we register a custom
menu and name it “Primary Menu”. Now, we will move on to implementing the
menu into our theme.
To do this, we will have to add this line of code below at the end of our
header.php document.
<?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'menu_class' =>
'nav', 'theme_location' => 'primary-menu' ) ); ?>
Now on to understanding exactly what this means. The primary function that is
being used is wp_nav_menu, with sort_column, container_class, and
theme_location as the arguments being used. What sort_column does is makes
sure that the order you pick in your WordPress dashboard is followed.
container_class will allow for you to choose the CSS class that you have created
to be used to style your menu. Lastly, theme_location just assigns the menu to
wherever we choose, which at the moment happens to be primary-menu.
We have our custom header navigation up and running, but at the moment it just
looks like a boring old list of links. To fix this, we will create a class called nav in
our style.css, now we will add the following code into our STYLE.css file.
.nav{ .nav ul ul a{
width:750px; height:auto;
background: #000; line-height:1em;
display:block; padding:10px;
float:left; width:130px;
position:relative; }
}
.nav li:hover > a,.nav ul ul:hover >
.nav ul{ a{
list-style:none; color:#ccc;
} }
.nav ul ul{
display:none;
position:absolute;
top:100%;
left:0;
float:left;
z-index:99999;
background: #212121;
}
Please note that above code is a simple code we usually write for navigation for
our templates.
Step 5 – Index.php
The index.php will be the home page of our website, and will contain code to
include our header, footer, and sidebar, which I will explain below, as well as the
code to include the most recent posts from our blog and take advantage of
WordPress's post thumbnails feature. Open index.php file and paste this code
there.
<?php get_header(); ?>
<div id="blog">
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<div class="post">
<h3><a href="<?php the_permalink(); ?>"><?php the_title();
?></a></h3>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content(); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?>
<?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment
»', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ',
''); ?>
</p>
</div>
</div>
<?php endwhile; ?>
<div class="navigation">
<?php posts_nav_link(); ?>
</div>
These lines of code are used to output all of the information in the header.php,
sidebar.php, and footer.php wherever you place them in your theme files.
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Step 6 – Enabling Post Thumbnails
We have added our code to show the post thumbnails on the homepage, but at
the moment nothing happens as we have not actually enabled the feature to
work. Now open up the functions.php that we worked on before, and the below
code should be added after your menu navigation code.
// Enable post thumbnails
add_theme_support('post-thumbnails');
set_post_thumbnail_size(520, 250, true);
The code above is pretty self-explanatory as it spells out almost exactly what it
does. The second line will add the support for post thumbnails in our theme,
while the third line defines the exact dimensions of our thumbnail, which for this
article, will be set at 520 pixels for width, and 250 pixels for height.
Step 7 – Sidebar.php
The sidebar.php is, as you guessed, the file that will display all of the information
we want in the sidebar. Since we have already included the file in our index.php,
all we have to do is put the following code in this file and our sidebar will show up
on the homepage.
<div class="sidebar">
<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar() ) : else :
?>
That will be all of the code that's added to our sidebar.php to make it functional.
We call the div that we created in our style.css, and the code below will make it
so that we can add widgets to our sidebar in the order and way we want them via
the WordPress backend. However, like many features, we have to add the
following code to our functions.php file first to make this feature work properly.
//Some simple code for our widget-enabled sidebar
if ( function_exists('register_sidebar') )
register_sidebar();
The code just tells WordPress to register a sidebar which we called in our
sidebar.php.
Step 8 – Single.php
The single.php is what will be used for a single post page, and most of the code
should look pretty similar since we've already coded up our index.php. Really the
only thing that's different is that we have added in our comments-template div,
and the code to include the comments.php.
Open the file single.php and add the following code inside that file.
<?php get_header(); ?>
<div id="blog">
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<div class="post">
<h3><a href="<?php the_permalink(); ?>"><?php the_title();
?></a></h3>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content(); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?>
<?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment
»', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ',
''); ?>
</p>
</div>
<div class="comments-template">
<?php comments_template(); ?>
</div>
</div>
<div class="navigation">
<?php previous_post_link('< %link') ?> <?php next_post_link(' %link
>') ?>
</div>
Step 9 – Page.php
When you create a page in WordPress, a different file is used to display the
content of what you typed into the page, and that is page.php. The code will look
almost completely identical to what we typed up in our single.php, except since it
is a page we are going to omit the comments template, and change the post
navigation slightly to handle a page instead of a post. Other than that, the code
will be exactly the same.
<div id="blog">
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<div class="post">
<h3><a href="<?php the_permalink(); ?>"><?php the_title();
?></a></h3>
<div class="entry">
<?php the_content(); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?>
<?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment
»', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ',
''); ?>
</p>
</div>
</div>
<div class="navigation">
<?php posts_nav_link(); ?>
</div>
Step 10 – Category.php
The category.php will serve as the file that, whenever a user looks at a specific
post category, time for posts, or specific author, will be the file that serves up the
information to display posts. As with our page.php, most of the code here is going
to be the exact same as the single.php we created before, except for a chunk
right at the beginning.
Open the category.php file and paste the following code inside it.
<?php get_header(); ?>
<div id="blog">
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<h2>Archive for the ‘<?php single_cat_title(); ?>’
Category:</h2>
<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
<h2>Posts Tagged ‘<?php single_tag_title(); ?>’</h2>
<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<h2>Archive for <?php the_time('F jS, Y'); ?>:</h2>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<h2>Archive for <?php the_time('F, Y'); ?>:</h2>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<h2>Archive for <?php the_time('Y'); ?>:</h2>
<?php /* If this is an author archive */ } elseif (is_author()) { ?>
<h2>Author Archive</h2>
<?php /* If this is a paged archive */ } elseif (isset($_GET['paged'])
&& !empty($_GET['paged'])) { ?>
<h2>Blog Archives</h2>
<?php } ?>
<div class="post">
<h3><a href="<?php the_permalink(); ?>"><?php the_title();
?></a></h3>
<div class="entry">
<?php the_content(); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?>
<?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment
»', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ',
''); ?>
</p>
</div>
</div>
<div class="navigation">
<?php posts_nav_link(); ?>
</div>
</div>
Step 11 – Footer.php
To finish off our work here, we are going to create our footer.php file, which too
will use the #footer that we declared in our style.css, and will just contain some
basic copyright information as well as a link to the stories and comments RSS feed
at the bottom.
<div id="footer">
<p>
<strong>Copyright 2011 <?php bloginfo('name'); ?> | All Rights
Reserved.</strong> </a>
Designed by <a href="#">testing</a>
</p>
<p><a href="<?php bloginfo('rss2_url'); ?>">Latest Stories RSS</a> | <a
href="<?php comments_rss_link('comment feed'); ?>">Comments RSS</a></p>
</div>
</div>
</body>
</html>
);
Post tags
Title <?php the_title(); ?>
$page_num = $paged;
?>
query_posts('cat=1&showposts=5&paged='.$page_num);
function ShortTitle($text)
$chars_text = strlen($text);
$text = substr($text,0,$chars_limit);
$text = $text."...";
return $text;
}
Post title text limit
add_theme_support('post-thumbnails');
the default limit of the (the_excerpt) is 53 and for customize this use this in function.php
add_filter('excerpt_more', 'new_excerpt_more');
Pervious post and next post link
<ul>
</ul>
if ( function_exists('register_sidebar') )
register_sidebar();
register_sidebar( array(
'description' => __( 'Widgets in this area will be shown on the right side.' ),
) );
Add new pages without side bar
Create a same copy of page.php (no-sidebar.php) and remove this <?php get_sidebar(); ?>
<?php
/*
*/
?>