HOWTO: Add a Custom Comment Class to WordPress

Ever need to add your own class to your WordPress comment template but not sure how because of the function comment_class that controls the classes? Check out the function comment_class and you will see adding your own custom classes very easily.

PHP:
  1. <?php comment_class('yourcustomclasshere'); ?>

Allow this sounds pretty basic - it is a lifesaver for doing a lot of customizations.

Show One WordPress Post

There is a great function you might be aware of that allows you to get one WordPress post and display it anywhere you would like (header, sidebar, home page, etc).

To show only one post, use the function get_post.

Example:

PHP:
  1. <?php $sdac_post_id = 10;
  2. $show_post_id = get_post($sdac_post_id);
  3. $sdac_title = $show_post_id->post_title;
  4. $sdac_content = $show_post_id->post_content;
  5. ?>
  6. <h2><?php echo $sdac_title; ?></h2>
  7. <?php echo $sdac_content ?>

The code above will show the title and content of the post with the post ID of 10. If you are not sure what your post ID is, go to the manage screen in the WP-Admin, and hover over the post title. In your browser status bar, you will see the URL - and the post ID is the number at the end of the URL.

Search Specific Category in WordPress

There are some times when you want to limit your search to a particular category, or perhaps multiple categories. This is relatively simple to do in WordPress by adding a hidden field to your search code. (See example below)

PHP:
  1. <form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
  2. <div><input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" />
  3. <input type="hidden" name="cat" value="22" />
  4. <input type="submit" id="searchsubmit" value="Search" />
  5. </div>
  6. </form>

You can see I added my hidden input field on the third line. When I add this in, it then adds onto the query used to search. Your search will go from something like http://www.sandboxdev.com/?s=WordPress to http://www.sandboxdev.com/?s=WordPress&cat=22 and will only return posts in the category ID you choose.