2013 最实用的Wordpress技巧、代码片段

2013 最实用的Wordpress技巧、代码片段

2013年的Wordpress发展很快。在各种论坛中有很多优秀的代码片段。我这里将我喜欢的一些技巧和片段整理出来,送给所有的Wordpress开发者;

使用剪裁而不是缩放来处理上传的图片

你是否想过,对你在Wordpress上传的图片进行剪裁成合适大小,而不是将他们缩放为合适的大小,如果想这么做,那么就看看下面的代码,你就能做到

将代码片段加入functions.php这个文件中:

// 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>&amp;])@([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]

不使用插件清理wp_head()

WordPress的通过wp_head()增添了不少的东西.很多主题也会与这个函数挂钩。但有些东西是完全用不到的。这里有一个简单的清理方法,可以清理无用的内容。 将代码片段加入functions.php这个文件中:

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,你可以通过以下代码来指定某个页面通过ssl来访问; 你需要做的就是将代码片段加入functions.php这个文件中,并指定特殊页面的id:

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]

在Wordpress之外循环访问页面

在WP之外,你需要访问文章信息,这里是一段代码,帮助你在wp之外的其他php文件上访问文章信息 将以下代码放在你需要访问的php页面下。并修改以下地方: line 4: 将你 WordPress wp-blog-header.php 文件正确路径填入. line 5: 通过 query_posts() 获取所需信息.

&lt;?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');
?&gt;</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]

转载于:https://my.oschina.net/u/265943/blog/292899

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值