Keep It Simple, Stupid.

荒野无灯weblog

Super useful WordPress hacks and snippets

1.免插件实现日志浏览数记录

function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

2.在contributors的日志发布后自动发邮件给他们

function wpr_authorNotification($post_id) {
   $post = get_post($post_id);
   $author = get_userdata($post->post_author);

   $message = "
      Hi ".$author->display_name.",
      Your post, ".$post->post_title." has just been published. Well done!
   ";
   wp_mail($author->user_email, "Your article is online", $message);
}
add_action('publish_post', 'wpr_authorNotification');

3.用短代码实现外链网站的快照显示

 '',
        'width' => 250,
    ), $attributes));

    $imageUrl = bm_mshot ($url, $width);

    if ($imageUrl == '') {
        return '';
    } else {
        $image = '' . $url . '';
        return '';
    }

}

function bm_mshot ($url = '', $width = 250) {

    if ($url != '') {
        return 'http://s.wordpress.com/mshots/v1/' . urlencode(clean_url($url)) . '?w=' . $width;
    } else {
        return '';
    }

}

add_shortcode('browsershot', 'bm_sc_mshot');
?>

使用方法,在日志中写上如:

[browsershot url="http://ihacklog.com" width="荒野无灯weblog"]

生成的效果图:



http://s.wordpress.com/mshots/v1/http%3A%2F%2Fihacklog.com?w=500

4.列出网络中的博客(适用于WP MU模式)


function wp_list_sites( $expires = 7200 ) {
   if( !is_multisite() ) return false;

   // Because the get_blog_list() function is currently flagged as deprecated
   // due to the potential for high consumption of resources, we'll use
   // $wpdb to roll out our own SQL query instead. Because the query can be
   // memory-intensive, we'll store the results using the Transients API
   if ( false === ( $site_list = get_transient( 'multisite_site_list' ) ) ) {
      global $wpdb;
      $site_list = $wpdb->get_results( $wpdb->prepare('SELECT * FROM wp_blogs ORDER BY blog_id') );
      // Set the Transient cache to expire every two hours
      set_site_transient( 'multisite_site_list', $site_list, $expires );
   }

   $current_site_url = get_site_url( get_current_blog_id() );

   $html = '
    ' . "\n"; foreach ( $site_list as $site ) { switch_to_blog( $site->blog_id ); $class = ( home_url() == $current_site_url ) ? ' class="current-site-item"' : ''; $html .= "\t" . '
  • ' . get_bloginfo('name') . '
  • ' . "\n"; restore_current_blog(); } $html .= '
' . "\n\n"; return $html; }

调用:



5.给有缩略图(thumbnail)的日志内容div添加一个css类选择器

function has_thumb_class($classes) {
    global $post;
    if( has_post_thumbnail($post->ID) ) { $classes[] = 'has_thumb'; }

        return $classes;
}
add_filter('post_class', 'has_thumb_class');

本文由荒野摘选和译自:http://www.catswhocode.com/blog/super-useful-wordpress-hacks-and-snippets

Tagged in :

All Comments (0)
Gravatar image
No Comments