WordPress十大优秀技巧,让你永久受用
自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,$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,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 显示“xxx久以前发布”Twitter上有个非常酷的功能就是可以显示一篇“推特”发表到现在已经多长时间。你也想在WordPress上实现这样的功能吗?在WordPress上也是可以实现滴。 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导航WordPress有一些函数允许你链接以前的文章。不过这些函数得在循环内使用。 Digging into WordPress这本书的作者Jeff Starr解决了这个问题。 <?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/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |