5 WordPress Tips and Tricks You Must Know – Part 2

Sep 18, 2009 13 Comments by

Hi again everyone,

If you have been following our previous post “5 WordPress Tips and Tricks You Must Know €“ Part 1” then you already know that it has covered the following topics:

  1. Adding a custom Read More button.
  2. Displaying post thumbnail using custom fields.

We continue our coverage for the remaining three tips and tricks that could be useful for new WordPress designers to add non-standard features to their themes. Let’s begin.

3) List sub categories and siblings in a single category page.

To list sub categories and siblings in a single category page; either on the top navigation menu or the side bar; the following code can be used:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<ul> if (is_category()) {
$this_category = get_category($cat);
$this_parent = $this_category-&gt;category_parent;&nbsp;

if (get_category_children($this_category-&gt;cat_ID) != "") {
wp_list_categories('hide_empty=0&amp;title_li=&amp;child_of=' . $this_category-&gt;cat_ID);
}
else {
if ($this_parent != 0) {
wp_list_categories('hide_empty=0&amp;title_li=&amp;child_of=' . $this_parent);
}
}
}
?&gt;</ul>

The code is enclosed within a <ul> tag; since all menu items will be displayed as <li> html tags.

We use the Conditional Tag is_category() to check if a Category archive page is being displayed. If so we retrieve the ID of this category and also the category parent ID. We then perform a simple check to find out if this category has any sub categories. If that is true; we display the sub categories. IF the condition returns no results; we simply us the category’s Parent ID to display the siblings of the category.

However, before displaying the sibling categories, we have to make sure that the categories parent is not the root page; otherwise all other categories under the root page will be displayed; definitely this is not the desired outcome.

4)     Displaying multiple posts list in a single page.

To display multiple posts list in a single page (i.e. Front Page [see next]); we modify the “index.php” page. However, before attempting to do this; it is recommended to copy the “index.php” file and paste it as “frontpage.php” in the same directory. We don’t to modify our original index.php file; as it will still be used for displaying the blog page.

The newly created “frontpage.php” should contain the original loop script. We are going to replace the loop code with our customized loop code to allow displaying multiple lists of posts. To do this we use the WordPress function query_posts() which Controls which posts show up in The Loop.

We use the query_posts() function to achieve some of the following common goals:

  • Display only a single post or page on your homepage
  • Show all posts from a particular time period
  • Show the latest post (only) on the front page
  • Change how posts are ordered
  • Show posts from only one category

Our purpose here is to display lists of posts for three different categories within the same page, the front page.

The code illustrated below can be used to arrive to the desired effects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<div class="recent-post-box"><!-- recent-post-box starts -->&nbsp;
<h1 class="heading-small">Recent Posts</h1>
while (have_posts()) : the_post(); ?&gt;
<div class="entry-small"><a title="Permanent Link to &lt;?php the_title_attribute(); ?&gt;" rel="bookmark" href="&lt;?php the_permalink() ?&gt;"><img class="thumbnail-small" src="&lt;?php echo get_post_meta($post-&gt;ID, 'post-thumbnail', true); ?&gt;" alt="Small Thumbnail" width="50" height="50" /></a>&nbsp;
<h2 class="title-small"><a title="Permanent Link to &lt;?php the_title_attribute(); ?&gt;" rel="bookmark" href="&lt;?php the_permalink() ?&gt;"></a></h2>
&nbsp;

</div>
&nbsp;

</div>
<!-- recent-post-box ends -->
<div class="tuts-post-box"><!-- tuts-post-box starts -->&nbsp;
<h1 class="heading-small">Tutorials Posts</h1>
while (have_posts()) : the_post(); ?&gt;
<div class="entry-small"><a title="Permanent Link to &lt;?php the_title_attribute(); ?&gt;" rel="bookmark" href="&lt;?php the_permalink() ?&gt;"><img class="thumbnail-small" src="&lt;?php echo get_post_meta($post-&gt;ID, 'post-thumbnail', true); ?&gt;" alt="Small Thumbnail" width="50" height="50" /></a>&nbsp;
<h2 class="title-small"><a title="Permanent Link to &lt;?php the_title_attribute(); ?&gt;" rel="bookmark" href="&lt;?php the_permalink() ?&gt;"></a></h2>
&nbsp;

</div>
&nbsp;

</div>
<!-- insp-post-box ends -->

<!-- recent-post-box ends -->
<div class="ipso-post-box"><!-- insp-post-box starts -->&nbsp;
<h1 class="heading-small">Inspirational Posts</h1>
while (have_posts()) : the_post(); ?&gt;
<div class="entry-small"><a title="Permanent Link to &lt;?php the_title_attribute(); ?&gt;" rel="bookmark" href="&lt;?php the_permalink() ?&gt;"><img class="thumbnail-small" src="&lt;?php echo get_post_meta($post-&gt;ID, 'post-thumbnail', true); ?&gt;" alt="Small Thumbnail" width="50" height="50" /></a>&nbsp;
<h2 class="title-small"><a title="Permanent Link to &lt;?php the_title_attribute(); ?&gt;" rel="bookmark" href="&lt;?php the_permalink() ?&gt;"></a></h2>
&nbsp;

</div>
&nbsp;

</div>
<!-- insp-post-box ends -->

5) Displaying customized front page.

As mentioned in the previous section; to have our customized front page, we copy the “index.php” file and paste in the same theme directory as a new file. You can give this new file any name. For the purpose of this tutorial, we named it “frontpage.php”.

For more information about WordPress pages you can visit the following link: http://codex.wordpress.org/Pages

Once you have prepared your frontpage.php, you can follow the following steps to configure your theme to display your front page.

First, your “frontpage.php” file should contain the following code at the very top before any other code:

1
2
3
4
5
6
7
8
/**
* @package WordPress
* @subpackage Default_Theme
*/

/*
Template Name: Front Page
*/

?&gt;

Next, in your WordPress admin screen, click the Pages link, then click the Add New link. The Edit Page section will be displayed; where you can type the name of your front page. We named it “Home”.

Leave the content area empty. Next, on the right hand side you will see the Attributes section. In this section, the parent drop down list will have “Main Page (no parent) value, keep it as it is.

Just below this drop down list, you will find the Template list item with the value “Default Template” selected. You will need to change this to the name of the template that is defined within the “frontpage.php” file. In this case it is “Front Page”, see below:

frontpage_admin

Once this is done, save your page and then publish it.

You will also be required to create a blog page that will display all your posts. To do this follow the same steps you carried out to create the front page. The only thing you need to do differently here is to name your new page “Blog” and then leave all other options as they are. In other words, in the Attributes section, the parent drop down list should have “Main Page (no parent) selected and the Template drop down list should have “Default Template” selected.

Save your page and publish it.

Next, locate the Reading link under the Settings section and apply the following changes:

  • Select the “A static page” radio button.
  • Select “Home” as your front page from the Front Page list item.
  • Select “Blog” as your blog page from the Posts Page list item.

See image below for more details:

frontpage_admin_reading

Save your changes and you should have a customized front page displayed as the first page for your blog.

This wraps up our tutorial on how to tweak WordPress features to achieve non-standard functionality. We hope that you have enjoyed our tutorial, and please don’t hesitate to write any feedback, suggestions or comments.

Featured, Tips, Tutorials, Wordpress-tuts

About the author

Love Freedom; Aiming to be the best Web Designer/Developer.

13 Responses to “5 WordPress Tips and Tricks You Must Know – Part 2”

  1. j000 says:

    Another impressive tutorial MAD I love these series I’m sure a lot of people out there will benefit from them. Thanks and keep them coming.

  2. Bruce H says:

    Can you tell me how you got the buttons to appear at the bottom of every blog post for sharing this article with the world? I’m referring to the Twitter, Plurk, etc. buttons at the bottom.

    Thanks,
    Bruce.

  3. James Morrison says:

    One thing to note regarding the Front Page template, you’ll end up with potential duplicate content issues (so example.com and example.com/home point to the same content).

    My advise is to hand code the index.php (or home.php) page rather than select a page.

    • MAD says:

      James,
      you are right, we encountered the same issue and resolved it by excluding the home page from the navigation menu manually.
      You can can hand code the index.php, but that would be too much coding and will make things a bit messy.

  4. ali says:

    very usefull thanks for sharing

  5. Diamond Drilling says:

    Everything is fine and thanks for sharing, but your website seems to be with errors (display & JS!) Keep up the good work!

  6. Benton Pressey says:

    I knew that research said that 97 % of weblogreaders just read and only 3 % responds, but it is good to see the reasons why those who don’t do this! Thanks and keep up the good work!

  7. Maudie Niheu says:

    Great post. I found what I was looking for. Do you mind if I post this on my website and give you credit? If not, it’s ok.