Tag Archives: Wordpress

Displaying posts in a category as a page in wordpress

In this site the Life page only displays the posts in the category Life.
I did this in a manual way by creating a Page Template for the Life page. This gives you more control. The problem with this approach is that you have to do it again for a new theme.

In wordpress, individual pages can be set to use a specific custom Page Template (a PHP template file, e.g., life.php) that we create within our Theme. The custom Page Template can be assigned while creating a new page. This new Page Template will then override the default page.php Page Template in the Theme.
When loading a page, wordpress looks first for the assigned Page template, then the page.php and finally the index.php. The first one it finds will be used to display that Page. The template pages are situated in the theme’s directory which is at wp-content/themes/your-theme

In our case we need a Page Template that will be similar to the home page but displays only the posts in the required category.
So we will base our custom Page Template on index.php which is the Page Template of the home page.

We make a copy of  the index.php with the name mypage.php (or anything.php)

Add the following code to the top of it so that wordpress understands that it is a Page Template and identify its Template Name. This name will be displayed in the theme editor page.

<?php
/*
Template Name: MyPage
*/
?>

Now, we add the filter to display only the required category. This should be added just after the above Template Name code

<?php
if (is_home()) {
query_posts($query_string."&cat=3");
}
?>

In this code we display only category with id 3. The category id can be found out from the wordpress Edit Category pages by mouse hovering the required category and its id will be shown in the browser’s status bar.
Now to display more than one category use some thing like this
query_posts($query_string."&cat=2,6,17,38");

Create a blank page and assign this custom template to it. [Look for a drop-down labeled “Page Template” towards the bottom]

Thats all!

For more detailed information on Page Templates, visit this wordpress codex page

Excluding a category from the front page in WordPress

This is a very well known trick where we use the query_posts() template tag to control which posts to show up. The code has to be placed above the Loop in the index.php template file.

<?php
if (is_home()) {
query_posts("cat=-3");
}
?>

The code checks whether we are in home page and if it is, it excludes the category with id 3 from displaying in it. The category id can be found out from the wordpres’s Edit Category pages by mouse hovering the required category and its id will be shown in the browser’s status bar.
More than one category could be excluded by using a code like this, query_posts("cat=-2,-6,-8")

However, i recently noticed that it breaks the pagination, and so the next and previous page links at the bottom only shows the same home page. Pagination is the feature by which we split the home page into pages based on the max number of posts that can be displayed in the page.

The problem was because, the call to query_posts() causes the wp_query object to generate a new SQL query using our parameters and ignores the other parameters it receives via the URL (page number etc).

The proper way is to send the parameters in the URL together with our parameters and this can be done by including the variable $query_string in the call to query_posts(). When doing this way the quoted portion of the argument must begin with an ampersand (&).
The code now looks like this

<?php
if (is_home()) {
query_posts($query_string."&cat=-3");
}
?>

Another technique is to use a code like this inside the Loop.
<?php if (in_category('3') && is_home() ) continue; ?>

I’ve not tested this code myself, but looking at it i think it should work fine.

For a detailed information on what the query_posts() is capable of doing, visit this wordpress codex page

Things to take care when starting a new personal blog/website

I just setup my first blog/website. These are a some of the steps that I took in the process of creating this blog. This is not an exhaustive list, but only the areas that I touched.

1. Find a good domain name. If your main audience is from search engines, and if you do not have any country specific audience, the .com .org or .net will give you better search engine position. However, if your audience is mainly from your country then choose the extension of that country. [For example:  .co.in sites have prominence over others in google.co.in]
The URL can have numbers, alphabets and ‘-‘[hyphen]. Make it short, meaningful [not like mine ;) ] and simple.

2. Find a good host. Look for decent bandwidth, diskspace, number of databases allowed, POP/SMTP email accounts, FTP support, control panel like cPanel etc. Do not go behind offers like unlimited diskspace/bandwidth etc. I just made that mistake and is paying for that. If you choose a Linux based host[like mine], make sure they support .htaccess files, and Apache mod_rewrite. These are required to create ‘pretty links‘. If your host provides a demo account, try it out for sure.

3. Choose a good Content Management software. I use WordPress for my blog and I highly recommend it. WordPress is the best if you are thinking of a blog.

4. Use an FTP client to upload/download all your data to/from the webserver. I Use Fillezilla.

5. Carefully choose a good set of passwords. It should be atleast 8 characters long and a mix of letters, alphabets and symbols. Use different passwords for ftp, email, database etc. Note it down in paper.

6. Have a basic understanding of search engine optimization(SEO) before adding posts. These are simple things like providing a good title for all the pages, adding meta description and meta keyword tags for all the pages, providing title attribute for your links and adding alt tag for your images.

7. Get some idea about traffic statistics tools. Your webserver may be providing one, but that may not be good. I use the stats analyzer of WordPress. Some others include Google analytics, Awstats, etc.

8. Turn Off directory listing if your webserver supports it. If not, add a blank index.html page to all the directories which are listed.

9. Think of a back up at least once in a week.