WordPress Theme Tweaks

WordPress Tutorialson January 17th, 200915 Comments

I have noticed that there is no such thing as perfect suitable WordPress theme for your needs. No matter what theme you’ll be downloading you will want to change something in it. To add a few extra options may be tricky sometimes but here are some WordPress theme hacks what will make your life easier.

Dynamic Content to homepage

If you want to include a specific file in your homepage you can use this code:

<?php if ( is_home() ) { include (‘yourfile.php’); } ?>

where ‘yourfile.php’ is the file that you want to include in the homepage

Use different post templates for different categories

Lets say you have a video, product or author category or any other where you want to have a different layout from your blog category see the categories ID’s, build single.php files with different layouts for your categories and rename each one according to the category ID like this single.1php, single2.php where 1 and 2 is the category name in our example and add this code in the base single.php file:

<?php
$post = $wp_query- >post;

if ( in_category(’1′) ) {
include(TEMPLATEPATH . ‘/single1.php’);

} elseif ( in_category(’2′) ) {
include(TEMPLATEPATH . ‘/single2.php’);

} else {
include(TEMPLATEPATH . ‘/single.php’);

}
? >

In the code insert the category ID in “in_category(’2′)”.

Display Featured Posts

This function is usually used in magazine style themes to display featured posts from a specific category in the homepage. You can use this function as many times as you want to have different categories featured in the homepage.

Example 1 – Display the summary (the excrept) and the title:

<?php query_posts(‘cat=2&showposts=5′); ?>
<?php while (have_posts()) : the_post(); ?>
<div class=”featured_post”>
<h3><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></h3>
<?php the_excerpt(__(‘Read More »’));?>
<hr />
</div>
<?php endwhile;?>

Example 2 – List titles from a specific category.

<?php query_posts(‘cat=2&showposts=5′); ?>
<?php while (have_posts()) : the_post(); ?>
<div class=”featured_post_list”>
<ul>
<li><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></li>
</ul>
</div>
<?php endwhile;?>

Change the category ID and the number of post you want to display in this line “(‘cat=2&showposts=5′)” where the category ID is 2 and the number of post displayed are 5, and customize the look of the classes “featured_post” and “featured_post_list” from your style CSS.

Display different templates for pages

Have some specific design preferences for your static pages? Build the templates for them and add these line first thing in your page template file.

<?php
/*
Template Name:  Contact Us
*/
?>

Now when you write a static page got to Attributes and choose the template you want to use for that specific page from Page Templates. In our example will be Contact Us

Display External RSS feed

It is not too hard to include external RSS feeds in your page. Use this code to list the latest RSS titles from an external RSS source.

<?php include_once(ABSPATH.WPINC.’/rss.php’);
wp_rss(‘http://www.externalrsssource.com/rss’, 5); ?>

where http://www.externalrsssource.com/rss is the RSS source and 5 is the number of titles listed.

Display latest tweet from Twitter

Put this code in your sidebar or wherever you want to display your latest tweet from Twitter.

<?php
// Your twitter username.
$username = “TwitterUserName”;
// Prefix – some text you want displayed before your latest tweet.
// (HTML is OK, but be sure to escape quotes with backslashes: for example href=\”link.html\”)
$prefix = “”;
// Suffix – some text you want display after your latest tweet. (Same rules as the prefix.)
$suffix = “”;
$feed = “http://search.twitter.com/search.atom?q=from:” . $username . “&rpp=1″;
function parse_feed($feed) {
$stepOne = explode(“<content type=\”html\”>”, $feed);
$stepTwo = explode(“</content>”, $stepOne[1]);
$tweet = $stepTwo[0];
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>

Change the “TwitterUserName” with your Twitter user name and if you want to have some text before and after your tweet insert the text here: $prefix = “” (before) and $suffix = “”; (after).

Add custom NewsLetter from FeedBurner

Keep your visitors updated and make them come back to your site. The NewsLetter form from FeedBurner is not very attractive so you perhaps want to build a custom NewsLetter form what is blending nicely with your theme. use the following code for this:

<div class=”newsletter”>
<h3>Subscribe to NewsLetter</h3>
Get the latest News and Updates
<form id=”subscribe_news” action=”http://www.feedburner.com/fb/a/emailverify” method=”post” target=”popupwindow” onsubmit=”window.open(‘http://www.feedburner.com’, ‘popupwindow’, ‘scrollbars=yes,width=550,height=520′);return true”>
<p><input type=”text” value=”Enter your email address…” id=”newsletter_textfield” onfocus=”if (this.value == ‘Enter your email address…’) {this.value = ”;}” onblur=”if (this.value == ”) {this.value = ‘Enter your email address…’;}” name=”email”/>
<input type=”hidden” value=”http://feeds.feedburner.com/~e?ffid=FeedBurnerSubscribeID” name=”url”/>
<input type=”hidden” value=”NewsLetter” name=”title”/>
<input type=”submit” value=”GO” id=”newsletter_button” /></p></form>
</div>

where you will change “FeedBurnerSubscribeID” with your subscribe ID provided by FeedBurner. The main form can be customized from your CSS file and have the id “subscribe_news”, the text field have the id “newsletter_textfield” and the button have the id “newsletter_button”. All form is wrapped in the div class “newsletter”.

15 Responses to “WordPress Theme Tweaks”

  1. rudy says:

    Hello siX,
    Thank you for this tutorial. It’s really useful for me in learning how to tweak wp theme.

  2. ricland says:

    I want to use a page for my home page.

    what file do I modify and what’s the code.

    Above you don’t identify the files to be modified, you just give the code

    • siX says:

      Yeah, it is a bit advanced but to explain each tweak from a to z take a post at least for each one. I might do this in the future. If you want to integrate the featured post display code look for home.php if not exist than index.php from your theme folder. I see you are using Brian Gardner’s theme so you should find a home.php. The homepage hierarchy is home.php and index.php as second. read more about this here.

      If you just want to use a static page as your homepage got to WordPress dashboard – reading and select “A static page” from “Front Page displays” then select the page you want to display.

  3. ricland says:

    SiX:

    This may be a bug, but when I select static page in the dashboard, it switches it, but the other page links don’t work from the new homepage.

    Therefore, I’m thinking I need to make the change in the theme editor.

    So where am I changing the code and what code am I changing to get the homepage a static page.

    Thanks — and great theme!

    ric

  4. cristin says:

    oops/ I was trying to download a new theme that I liked to wp-content/themes in my c panel. After I added it I didn’t see it and then deleted the classic and default out of the theme file………yes this is my first time creating a blog – what do I do to fix it? Thanks

    • siX says:

      Hi Cristin,
      Do not worry, go to WordPress, download it again and upload the whole themes folder from wp-content and replace yours. It will add the classic and default theme again. Upload any other theme you need and go to yourdomain.com/wp-admin

      The login window will appear, log in to your wp panel and go to Appearance. Now select any theme you want. If by mistake you have deleted other files also it’s not the end of the world, you can replace them but from wp-config.php connect to the same database that you have created and you will not loose any articles.

      Happy blogging!

  5. Netty Gritty says:

    Hello siX,

    Thank you very much indeed for the snippets. I am very new to the world of wp theme designers and I have benefited a lot from the info in this post.

  6. Tanim Shahriar says:

    Thanks for this helpful article. I think this will help many people who are new to wordpress. In my blog I use many categories and I also need various modified templates for particular categories. So, would you suggest me how to add and use various Archives templates (e.g. archive.php, archive1.php. archive2.php…. ) for various categories???

  7. Devilslab says:

    hey nice work buddy…..keep it up…i am definitely using your themes and subscribing your posts

  8. VirsLee says:

    Thank you a lot for posting this! I tried the “Display different templates for pages” and it works great. It took a while to figure it out LOL , I was confused (not sure why) with the name of the template file. I had no idea what to name it. Well it can be anything.php if you want it, in the attributes fields will appear what is written inside the template file as the Template Name: Contact Us

  9. Roy says:

    Thanks you for this post. Its a real aid to WP newbies like me. Quick question, if I find a theme that i like (layout) but not the colors, where do i edit those? and is there an editor that I can use to achieve this? Thanks alot

  10. Andy says:

    Hi
    Just a little confused on Themes Options Page – Featured Content IMG: and Featured Content Link:

    What do I need in here can you give examples, just come to a mental roadblock, your help would be appreciated

  11. don luttrull says:

    I need to go back through the code but man, great tweaks for sure.

Leave a Reply