2013年的Wordpress发展很快。在各类论坛中有不少优秀的代码片断。我这里将我喜欢的一些技巧和片断整理出来,送给全部的Wordpress开发者; php
你是否想过,对你在Wordpress上传的图片进行剪裁成合适大小,而不是将他们缩放为合适的大小,若是想这么作,那么就看看下面的代码,你就能作到 css
将代码片断加入functions.php这个文件中: web
// Standard Size Thumbnail if(false === get_option("thumbnail_crop")) { add_option("thumbnail_crop", "1"); } else { update_option("thumbnail_crop", "1"); }</p> <p>// Medium Size Thumbnail if(false === get_option("medium_crop")) { add_option("medium_crop", "1"); } else { update_option("medium_crop", "1"); }</p> <p>// Large Size Thumbnail if(false === get_option("large_crop")) { add_option("large_crop", "1"); } else { update_option("large_crop", "1"); }来源:[weblink url="http://wp-snippet.com/snippets/activate-cropping-for-all-thumbnail-sizes/"]cropping-for-all-thumbnail[/weblink] ##在Wordpress 自动连接twitter用户名
将代码片断加入functions.php这个文件中: 服务器
function twtreplace($content) { $twtreplace = preg_replace('/([^a-zA-Z0-9-<em>&])@([0-9a-zA-Z</em>]+)/',"$1<a href=\\"http://twitter.com/$2\\" target=\\"_blank\\" rel=\\"nofollow\\">@$2</a>",$content); return $twtreplace; }</p> <p>add_filter('the_content', 'twtreplace'); <br /> add_filter('comment_text', 'twtreplace');来源: [weblink url="http://snipplr.com/view/70977/automatically-link-twitter-usernames-in-wordpress/"]automatically-link-twitter[/weblink]
WordPress的经过wp_head()增添了很多的东西.不少主题也会与这个函数挂钩。但有些东西是彻底用不到的。这里有一个简单的清理方法,能够清理无用的内容。 将代码片断加入functions.php这个文件中: ide
remove_action( 'wp_head', 'rsd_link' ); remove_action( 'wp_head', 'wlwmanifest_link' ); remove_action( 'wp_head', 'wp_generator' ); remove_action( 'wp_head', 'start_post_rel_link' ); remove_action( 'wp_head', 'index_rel_link' ); remove_action( 'wp_head', 'adjacent_posts_rel_link' ); remove_action( 'wp_head', 'wp_shortlink_wp_head' );来源:[weblink url="http://www.themelab.com/remove-code-wordpress-header/"]remove-code-wordpress-header[/weblink]
当你服务器上面启用了ssl,你能够经过如下代码来指定某个页面经过ssl来访问; 你须要作的就是将代码片断加入functions.php这个文件中,并指定特殊页面的id: wordpress
function wps_force_ssl( $force_ssl, $post_id = 0, $url = '' ) { if ( $post_id == 25 ) { return true } return $force_ssl; } add_filter('force_ssl' , 'wps_force_ssl', 10, 3);来源:[weblink url="http://wpsnipp.com/index.php/functions-php/force-specific-pages-to-be-secure-ssl-https/"]specific-pages-https[/weblink]
在WP以外,你须要访问文章信息,这里是一段代码,帮助你在wp以外的其余php文件上访问文章信息 将如下代码放在你须要访问的php页面下。并修改如下地方: line 4: 将你 WordPress wp-blog-header.php 文件正确路径填入. line 5: 经过 query_posts() 获取所需信息. 函数
<?php // Include WordPress define('WP_USE_THEMES', false); require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php'); query_posts('posts_per_page=1'); ?></p> <?php while (have_posts()): the_post(); ?> <h2><?php the_title(); ?></h2> <?php the_excerpt(); ?> <p><a href="<?php the_permalink(); ?>" class="red">Read more...</a></p> <?php endwhile; ?>来源:[weblink url="http://css-tricks.com/snippets/wordpress/run-a-loop-outside-of-wordpress/"]loop-outside-of-wordpresss[/weblink]