Super useful WordPress hacks and snippets

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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的日志发布后自动发邮件给他们

1
2
3
4
5
6
7
8
9
10
11
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.用短代码实现外链网站的快照显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
function bm_sc_mshot ($attributes, $content = '', $code = '') {

    extract(shortcode_atts(array(
        'url' => '',
        'width' => 250,
    ), $attributes));

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

    if ($imageUrl == '') {
        return '';
    } else {
        $image = '<img src="' . $imageUrl . '" alt="' . $url . '" width="' . $width . '"/>';
        return '<div class="browsershot mshot"><a href="' . $url . '">' . $image . '</a></div>';
    }

}

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');
?>

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

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

生成的效果图:


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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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 = '
<ul id="network-menu">'
. "\n";

   foreach ( $site_list as $site ) {
      switch_to_blog( $site->blog_id );
      $class = ( home_url() == $current_site_url ) ? ' class="current-site-item"' : '';
      $html .= "\t" . '
<li id="site-'
. $site->blog_id . '" '="" .="" $class=""><a href="' . home_url() . '">' . get_bloginfo('name') . '</a></li>

' . "\n";
      restore_current_blog();
   }

   $html .= '
</ul>

<!--// end #network-menu -->' . "\n\n";

   return $html;
}

调用:

1
2
3
4
5
6
7
8
9
10
11
<?php
// Multisite Network Menu
$network_menu = wp_list_sites();
if( $network_menu ):
?>
<div id="network-menu">
   <?php echo $network_menu; ?>
</div>

<!--// end #network-menu -->
<?php endif; ?>

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

1
2
3
4
5
6
7
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

更多
2 Responses Post a comment
  1. Demon

    收藏。第一个我从一个主题扒下过。不过好像不能保留数据的。

Leave a Reply

Note: You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS