自2010年開端以來關于WordPress的使用技巧網上層出不窮。在這篇文章里,我取最為精華的十個WordPress技巧絕對值得大家嘗試!
我特別喜歡像這個博客一樣統計文章的數量,隨著自己每寫一篇文章,看著數字在增長非常有成就感。下面這個方法就是教大家如何通過使用自定義字段在自己的博客上實現同樣的功能。
這個辦法執行起來非常簡單,首先,在你的functions.php文件里添加下面的代碼:
function updateNumbers() {global $wpdb;$querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ";$pageposts = $wpdb->get_results($querystr, OBJECT);$counts = 0 ;if ($pageposts):foreach ($pageposts as $post):setup_postdata($post);$counts++;add_post_meta($post->ID, 'incr_number', $counts, true);update_post_meta($post->ID, 'incr_number', $counts);endforeach;endif;}add_action ( 'publish_post', 'updateNumbers' );add_action ( 'deleted_post', 'updateNumbers' );add_action ( 'edit_post', 'updateNumbers' );
添加完成之后,你可以通過下面的代碼顯示文章號,注意下面的代碼必須在循環里使用。
<?php echo get_post_meta($post->ID,'incr_number',true); ?>
來源: http://www.wprecipes.com/how-to-display-an-incrementing-number-next-to-each-published-post
如果你像本文的作者一樣,你的博客有其他客串文章,那么可能會覺得用戶無法上傳文件是比較遺憾的。因為大多數博客還是需要圖片使文章更加吸引人。因此下面這個技巧就會顯得非常方便: 只要在function.php文件里添加下面的代碼,你的用戶就可以在WordPress管理后臺上傳文件了,夠酷吧?
if ( current_user_can('contributor') && !current_user_can('upload_files') ) add_action('admin_init', 'allow_contributor_uploads'); function allow_contributor_uploads() { $contributor = get_role('contributor'); $contributor->add_cap('upload_files'); }
來源: http://www.wprecipes.com/wordpress-tip-allow-contributors-to-upload-files
Twitter上有個非常酷的功能就是可以顯示一篇“推特”發表到現在已經多長時間。你也想在WordPress上實現這樣的功能嗎?在WordPress上也是可以實現滴。
只要在functions.php文件上粘貼這個代碼,保存之后,只要是二十四小時內發布的文章就會顯示“xxx久以前發布“而不是普通的發布時間。
add_filter('the_time', 'timeago');function timeago() {global $post;$date = $post->post_date;$time = get_post_time('G', true, $post);$time_diff = time() - $time;if ( $time_diff > 0 && $time_diff < 24*60*60 )$display = sprintf( __('%s ago'), human_time_diff( $time ) );else$display = date(get_option('date_format'), strtotime($date) );return $display;}
來源: http://aext.net/2010/04/display-timeago-for-wordpress-if-less-than-24-hours/
WordPress有一些函數允許你鏈接以前的文章。不過這些函數得在循環內使用。 Digging into WordPress這本書的作者Jeff Starr解決了這個問題。
只要粘貼下面的代碼到single.php文件,或者更好的辦法是干脆把代碼放到單獨一個php文件,然后將它放到主題文件夾下。
<?php if(is_single()) { // single-view navigation ?><?php $posts = query_posts($query_string); if (have_posts()) : while (have_posts()) : the_post(); ?><?php previous_post_link(); ?> | <?php next_post_link(); ?><?php endwhile; endif; ?><?php } else { // archive view navigation ?><?php posts_nav_link(); ?><?php } ?>
來源: http://digwp.com/2010/04/post-navigation-outside-loop/