This is the second part of my simple wordpress tutorial, this will go through showing the blog post, the comments section and the search functionally. Putting this tutorial together with the last you’ll have a completely unstylized functioning blog, which you can add all the styling you want.
So now onto single.php, this is the code to display how a single post is shown. First we’ll look at the code to insert the post.
We then extract the required information for the post using wordpress functions, the following commands are the most commonly used:
12345678
<?phpthe_title();// echo's the post title ?><?phpthe_author();// echo's the post author ?><?phpthe_content();// echo's the post content ?><?phpthe_time();// echo's the date/time of the post ?><?php// echo's the category of the post as well as a link to the category page ?><?phpthe_category();?><?php// echo's the tags of the post as well as a link to the tag page ?><?phpthe_tags();?>
Now the only other functions which we need to include in the single.php file and which we haven’t seen already is the worpdress function which pulls in the comments.php file and the functions for linking to the next and previous posts:
123
<?phpcomments_template();?><?phpprevious_post_link('%link','« Previous Post');?><?phpnext_post_link('%link','Next Post »')?>
The entire single.php follows a very similar pattern to the index.php so you should have no real problem following it, the comments.php file is slightly more complicated and probably the most complicated part of the wordpress system, but they still manage to make it quite simple for us. I’m going to split the file comments.php up into managable chucks and explain each chunk, heres the first:
1234567891011
<?php// Do not delete these linesif(!empty($_SERVER['SCRIPT_FILENAME'])&&'comments.php'==basename($_SERVER['SCRIPT_FILENAME']))die('Please do not load this page directly. Thanks!');if(post_password_required()){?> <p>This post is password protected. Enter the password to view comments.</p><?phpreturn;}?>
This is wordpress’s own code, do not touch this. It pretty much stops people from accessing the page directly and checks to see if you need a password to view comments, not really something we should worry about, but make sure you include it.
123456789101112
<?phpif(have_comments()):?> <h3><?phpcomments_number('No Responses','One Response','% Responses');?> to "<?phpthe_title();?>"</h3><?phpprevious_comments_link()?><?phpnext_comments_link()?> <ol><?phpwp_list_comments('avatar_size=48');?> </ol><?phpprevious_comments_link()?><?phpnext_comments_link()?>
This chunk of code tests whether there are comments in the wordpress query, and if there is, then the code first of all uses
to print out a statement depending on how many comments there are, the parameters given to the function show quite nicely exactly what the function does and what each parameter does.
Next we provide a link to pages of comments for when pagination is used and then we output the comments using this handy WordPress function:
1
<?phpwp_list_comments('avatar_size=48');?>
The parameter given here tells us what sixe to put the avatar images on the site, there are a few other parameters and you should probably check out on the wordpress codex, but for our simple blog we don’t need it.
Next we have the chunk of code that will generally just display nothing, but you might want to put in a message saying that there are no comments or something like that here.
This chunk outputs the form where comments are inputted, its quite long because it displays a slightly different form if the user is already logged in. Which is easily checked using:
<?phpif(comments_open()):?><?phpif(get_option('comment_registration')&&!is_user_logged_in()):?> <p>You must be <a href="<?phpechoget_option('siteurl');?>/wp-login.php?redirect_to=<?phpthe_permalink();?>">logged in</a> to post a comment.</p><?phpelse:?> <form action="<?phpechoget_option('siteurl');?> /wp-comments-post.php" method="post"> <fieldset> <legend><?phpcomment_form_title('Leave a Comment','Leave a Reply to %s');?></legend><?phpif(is_user_logged_in()):?> Logged in as <a href="<?phpechoget_option('siteurl');?>/wp-admin/profile.php"><?phpecho$user_identity;?></a>. <a href="<?phpechoget_option('siteurl');?>/wp-login.php?action=logout" title="Log out of this account">Logout »</a> <label>Comment: <textarea name="comment" id="comment" cols="50" rows="20"> </textarea></label><?phpendif;?><?phpif(!is_user_logged_in()):?> <label>Name: <em>Required</em> <input type="text" name="author" id="author" value="<?phpecho$comment_author;?>" /></label> <label>Email: <em>Required, not published</em> <input type="text" name="email" id="email" value="<?phpecho$comment_author_email;?>"/></label> <label>Homepage: <input type="text" name="url" id="url" value="<?phpecho$comment_author_url;?>" /></label> <label>Comment: <textarea name="comment" id="comment" cols="50" rows="20"> </textarea></label><?phpendif;?> <input type="submit" value="Post Comment" /> <input type="hidden" name="comment_post_ID" value="<?phpecho$id;?>" /> </fieldset><?phpdo_action('comment_form',$post->ID);?> </form><?phpendif;?><?phpendif;// if you delete this the sky will fall on your head ?>
The only thing you might not understand is the final function:
1
<?phpdo_action('comment_form',$post->ID);?>
which pretty much tells the form what to do with the information. And so there we have it the most simple comments implementaation in the wordpress framework. One last bit to go over is the nice search functionality wordpress has. This is implemented using a theme’s search.php file. I’m not going to go too much in depth into this as it is very similar to index.php but I thought I would show you the code just to round off this article, so here you go:
<?phpget_header();?><form method="get" id="searchform" action="<?phpbloginfo('url');?>/"><input type="text" value="<?phpthe_search_query();?>" name="s" id="s" /><input type="submit" id="searchsubmit" value="Search" /></form><h2>Search Results</h2><?phpif(have_posts()):?><?phpwhile(have_posts()):the_post();?> <h3><a href="<?phpthe_permalink()?>" rel="bookmark"><?phpthe_title();?></a></h3><?phpthe_excerpt('Read the rest of this entry »');?><?php$year=get_the_time('Y');$month=get_the_time('m');$day=get_the_time('d');?> <a href="<?phpechoget_day_link("$year","$month","$day");?>"><?phpthe_time('F j, Y');?></a> Filed Under: <?phpthe_category(', ');?><?phpthe_tags('Tags:',' : ','');?><?phpendwhile;?><?phpnext_posts_link('« Older')?><?phpprevious_posts_link('Newer »')?><?phpelse:?> Not Found <p>Sorry, but you are looking for something that isn't here.</p><?phpendif;?><?phpget_sidebar();?><?phpget_footer();?>
I hope this will help when designing your new wordpress blog, happy coding!