Wordpress theme control hacks

Sun, Jan 18, 2009

CMS tutorials, Wordpress tutorials

Wordpress theme control hacks

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”

Reblog this post [with Zemanta]

Related Posts

  1. Drupal theme making
, , , , ,

2 Responses to “Wordpress theme control hacks”

  1. Anonymous Says:

    Great hacks & very helpful, thanks a lot for the info.

  2. skiste Kit Says:

    Hey very nice blog!! Man .. Beautiful .. Amazing .. I will bookmark your blog and take the feeds also…


Leave a Reply