Create A Latest Post Section At The Beginning Of Each Post

I get this idea from SmashingApps. They created a latest article section at the top of each article. You may refer to the screenshot below:

This will make sure those readers who reach your blog through search engine or referer will have a chance to read the latest article. So, how are you going to add this section? Do you need any plugin to do so? No, you don’t need any plugin to add this section. Simply follow the tutorial below, and i will create a custom function for you.

  1. First, open your template’s functions.php using any text editor.
  2. Place this code at the end of the file. (Before “?>“)
    /* Get latest post */
    function get_latest_post($pre="You maybe interested in our latest article") {
    global $wpdb;
    $sql = "
    SELECT P.ID, P.post_title
    FROM $wpdb->posts P
    WHERE P.post_status = 'publish'
    AND P.post_type = 'post'
    ORDER BY P.ID DESC
    LIMIT 1
    ";
    $post = $wpdb->get_row($sql);
    $ret = $pre;
    $ret .= '<a href="'. get_permalink($post->ID).'" title="'. $post->post_title .'"> '. $post->post_title .'</a>';
    echo $ret;
    }
  3. Basically, the code will query the latest published post, get the permalink and post title, and then append it with some text (the default is “You maybe interested in our latest article“).
  4. Open your single.php and place this code before the “entry” div.
    <div id="latest-post">
    <p><?php get_latest_post("You may want to read our latest article"); ?></p>
    </div>
  5. You may change the “You may want to read our latest article” to any text that you want. Leave it blank and you will use the default setting.

You may call this function anywhere in your article or change the style through your template’s stylesheet.

3 Methods To Exclude Wordpress Categories In A Post

Sometimes, we may want to exclude some categories from display in our main page or sidebar. There are a lot of methods to exclude these categories. Here i would like to share 3 types of different method to do so.

Use Wordpress Category Visibility Plugin

This plugin will let you to exclude any tag and category at different pages. For example, you can exclude selected category in the front page, search results, archives, and feeds. Simply activate the plugin and go to your Manage->Category Visibility to manage them. (There are some users who claim this plugin may has conflict with other Wordpress plugins.)

wp_list_categories() template tags

You may use this template tags to list out all categories at your website’s navigation bar or your sidebar. The template tags tutorial website actually teach us how to include or exclude categories by adding parameters when we call this template tags.

Hack the default widgets.php

I read this tutorial from Simple Solution For smart People website. You need to make a backup of wp-includes/widgets.php file before proceed.

  1. After you backup the file. Open it using any text-editor and try to look for function wp_widget_categories. I am using Wordpress 2.6.x and it is around line 703. This is the only function that you need to modify.
  2. Find the following line:
    $d = $options[$number]['dropdown'] ? '1' : '0';

    Add $exclude = ‘17′; below this line. You may replace 17 with the Category ID that you want to exclude. You may exclude more than one category by adding a “,” between the number.

  3. Next, look for
    $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);

    And replace it with

    $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h, 'exclude' => $exclude);

Done. You can change the “17″ to any category ID that you want to exclude anytime. I had pasted my codes at the end of this tutorial, in case you don’t understand my tutorial.

function wp_widget_categories($args, $widget_args = 1) {
    extract($args, EXTR_SKIP);
    if ( is_numeric($widget_args) )
        $widget_args = array( 'number' => $widget_args );
    $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    extract($widget_args, EXTR_SKIP);

    $options = get_option('widget_categories');
    if ( !isset($options[$number]) )
        return;

    $c = $options[$number]['count'] ? '1' : '0';
    $h = $options[$number]['hierarchical'] ? '1' : '0';
    $d = $options[$number]['dropdown'] ? '1' : '0';
    $exclude = '17';

    $title = empty($options[$number]['title']) ? __('Categories') : apply_filters('widget_title', $options[$number]['title']);

    echo $before_widget;
    echo $before_title . $title . $after_title;

    $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h, 'exclude' => $exclude);

    if ( $d ) {
        $cat_args['show_option_none'] = __('Select Category');
        wp_dropdown_categories($cat_args);
?>

Hope you enjoying reading my tutorial. Please choose any method that suit you.

How To Export Articles From A Selected Category

The Wordpress CMS comes with Export/Import feature for us to export our blog posts from one website and import to another website. However, the default Export feature only allow us to export posts from a selected author.

I found one interesting request from digitalpoint forum. The user requests someone to modify the default Wordpress files so that he can exports posts from a selected category. I had tried to modify the files, found the solution and i would like to share it with my readers (I didn’t make any profit from him and someone had already sold his solution for that guy).

We need to modify the wp-admin/export.php and wp-admin/includes/export.php. Make sure you do a backup of your Wordpress database and these two files before you continue.

wp-admin/export.php

First of all, let’s start with wp-admin/export.php. Open your with wp-admin/export.php with any text editor, and search for

if ( isset( $_GET['download'] ) ) {
export_wp( $_GET['author'] );
die();
}

Replace with

if ( isset( $_GET['download'] ) ) {
export_wp( $_GET['category'] );
die();
}

Next, search for

<table class="form-table">
<tr>
<th><label for="author"><?php _e('Restrict Author'); ?></label></th>
<td>
<select name="author" id="author">
<option value="all" selected="selected"><?php _e('All Authors'); ?></option>
<?php
$authors = $wpdb->get_col( "SELECT post_author FROM $wpdb->posts GROUP BY post_author" );
foreach ( $authors as $id ) {
$o = get_userdata( $id );
echo "<option value='$o->ID'>$o->display_name</option>";
}
?>
</select>
</td>
</tr>
</table>

Replace with

<table class="form-table">
<tr>
<th><label for="category"><?php _e('Restrict Category'); ?></label></th>
<td>
<select name="category" id="category">
<option value="all" selected="selected"><?php _e('All Categories'); ?></option>
<?php
$categories = (array) get_categories('get=all');
foreach ( $categories as $c ) {
echo "<option value='$c->term_id'>$c->cat_name</option>";
}
?>
</select>
</td>
</tr>
</table>

wp-admin/includes/export.php

Next, open your wp-admin/includes/export.php and search for

function export_wp($author='') {

Replace with

function export_wp($cat='') {

Next, search for

if ( $author and $author != 'all' ) {
$author_id = (int) $author;
$where = $wpdb->prepare(" WHERE post_author = %d ", $author_id);
}

Replace with

if ( $cat and $cat != 'all' ) {
$cat_id = $cat;
$ids = $wpdb->get_col("SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id=$cat");
$where = $wpdb->prepare(" WHERE ID IN(".join(',',$ids).")");
}

Explanation

Basically, what you are doing is just replace the “author” with “category“. The default codes will query the available authors while my codes query the available categories.

The second file plays the most important role. It is a bit complicate to explain step by step. But basically, I will query the post ID that match the selected category ID and save the SQL query string in a $where variable. The $where string will be used in the subsequent codes, which we no need to modify.

I hope you guys can understand what i trying to explain. I had prepared a zip file that contains both of the modified files. You may download it and replace with the original files.

Note: I had tried this hack on Wordpress 2.6.2 only and i don’t know whether it works on other version or not. Please make a backup of these two files before you try!

Download: wp-admin.zip