Wordpress

My wordpress hacks

After spending some hours messing aroung Wordpress template, tags, plugins, themes, etc… I was able add some cool features to my web log. I’m posting here the code of some of them:

Showing a page related posts

This piece of code will look for posts with tags equal to the page title. You can see this in action at the bottom of this very same page. Add the following to you template’s page.php.

<div class="entry">
<h2><?php wp_title('',true);?> related posts</h2>
<?php
 $tag=trim(wp_title('',false));
 $args = 'numberposts=-1&orderby=date&tag='.$tag;
 $lastposts = get_posts($args);
 foreach($lastposts as $post) :
 setup_postdata($post);
?>
<h3><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h3>
<?php endforeach; ?>
</div>

Showing a post related posts

This is the very same thing, but now for a single post. It lists the all posts with at least one equal tags. add the following to your template’s single.php.

 <div>
 <?php
 $thisposttitle=the_title_attribute('echo=0');
 $taglist="";
 $posttags = get_the_tags();
 if ($posttags) {
 foreach($posttags as $tag) {
 $taglist = $taglist . $tag->name . ', ';
 }
 }
 $taglist=substr_replace($taglist ,"",-2);
 echo '<h2>Other '.$taglist.' related posts</h2>';
 $args = 'numberposts=-1&orderby=date&tag='.$taglist;
 $lastposts = get_posts($args);
 foreach($lastposts as $post) :
 setup_postdata($post);
 if ($thisposttitle == the_title_attribute('echo=0')) continue;
 echo '<h3><a href="';the_permalink(); echo'" id="post-'; the_ID(); echo'">'; the_title_attribute(); echo '</a></h3>';
 endforeach;
 ?>
 </div>

Adding a Blog/Home link to navigation bar or page list

As far as I know, most of the navigation bars, some of them with drop down menus, and page lists usually located in the sidebar, are done calling the wp_list_pages() function. This function does not include the home page, that one containing your cronological posts (the blog).

Locate the file where those structures are created. You may find something like:

<div id="navigation">
<ul id="nav">
<?php wp_list_pages('sort=menu_order&depth=3&title_li=');?>
</ul>
</div>

To add the home page to the list, just add a table line with a link to that page, similar to:

<div id="navigation">
<ul id="nav">
<li><a href="<?php echo get_option('home'); ?>/" title="<?php bloginfo('name'); ?>">Blog</a></li>
<?php wp_list_pages('sort=menu_order&depth=3&title_li=');?>
</ul>
</div>



Wordpress related posts

Theme by RoseCityGardens.com modified by Ricardo Neves