Including dynamic content in WordPress

A recent problem found some interesting solutions – I wanted to show a feature on posts of a certain category only.
The feature was the excellent ratings widget from gdragon – the problem was that from their dashboard it was either all or nothing regarding which posts showed the widget.

So I included the following code in the loop (in my case on index.php and single.php) where I wanted the widget to appear

<?php if ( in_category('2') ) { include ('ratings_widget.php'); } ?>

…where ’2′ is the category ID for which posts I want it to show in.
I then created a file called ‘ratings_widget.php’ which included only the placement code for the widget. I then uploaded it with the theme’s php files (next to style.css) via ftp.
The result being that the code was only included in the page when the category was the one I wanted. Easy. Ish.

A similar process can be used to style a particular category of post by using this…

<?php if ( in_category('2') ) { echo ('class="style_for _category2"'); } ?>

(obviously change ‘style_for _category2′ to something better).

Or even include features on particular pages instead of categories with this…

<?php if ( is_home() ) { include ('feature.php'); } ?>

This will give huge flexibilty to layout and feature placement to your blog without having to place the code into every post or creating templates.

2 Comments

  1. Posted October 28, 2008 at 6:09 pm | Permalink

    I am the author of GD Star Rating plugin, and this is great solution for limiting rating to categories. I am planning to add managment panel for plugin to control this on category level. Right now you can control rating on post level, but with blogs with a lot of posts and a lot of categories this is a problem. You can expect integrated solution in the next 2-3 weeks tops.

  2. penfold
    Posted October 28, 2008 at 7:11 pm | Permalink

    Hi Milan – thank you for popping by and also for an excellent plugin.
    It’s brought real life to a recent project. I look forward to future releases…

Post a Comment

Your email is never shared. Required fields are marked *

*
*