10 個實用的 WordPress 技巧教程
來源:程序員人生 發(fā)布時間:2013-10-04 20:45:27 閱讀次數(shù):2796次
日前,網(wǎng)給大家介紹了《10 個簡單的 WordPress 技巧》,無巧不成書,今天又在芒果小站看到《10 個實用的 WordPress 技巧教程》,下邊我們一起來閱讀一下吧。
1、自動向 WordPress 編輯器插入文本
編輯當(dāng)前主題目錄的 functions.php 文件,并粘貼以下代碼:
<?php
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = "芒果小站 - 這里不賣芒果,請另尋他處購買。";
return $content;
}
?>
2、獲取 WordPress 注冊用戶數(shù)量
通過簡單的 SQL 語句,即可方便獲得 WordPress 注冊用戶的數(shù)量:
$users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
echo "總共有 ".$users." 位注冊用戶";
3、根據(jù)指定自定義字段獲取 WordPress 文章
在 query_posts() 函數(shù)中傳入自定義字段參數(shù),即可獲取對應(yīng)文章列表:
<?php query_posts('meta_key=review_type&meta_value=movie'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
參數(shù)中 meta_key 是索要獲取自定義字段名稱,meta_value 是自定義字段取值。
4、獲取某個時間段的 WordPress 文章
編輯 index.php 文件,只需在循環(huán)體之前,添加以下代碼即可。當(dāng)然需要根據(jù)需要更換時間段的設(shè)置:
<?php
function filter_where($where = '') {
$where .= " AND post_date >= '2009-01-01' AND post_date <= '2010-01-01'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>
5、為某個 WordPress 標(biāo)簽生成 RSS 訂閱源
如你所見,標(biāo)簽可以通過逗號分割,這樣也可以獲取多個標(biāo)簽的 RSS Feed 源:
<a href="http://www.mangguo.org/?feed=rss&tag=query_posts,loop">
6、防止緩存 WordPress 樣式文件
通過服務(wù)器端設(shè)置以防止客戶端讀取緩存文件:
<link rel="stylesheet" href=http://www.mangguo.org/"<?php bloginfo('stylesheet_url'); echo '?'.filemtime( get_stylesheet_directory().'/style.css'); ?>" >
7、用戶統(tǒng)計文章字?jǐn)?shù)的 WordPress 函數(shù)
在當(dāng)前主題的 functions.php 文件中粘貼以下代碼:
function wcount(){
ob_start();
the_content();
$content = ob_get_clean();
return sizeof(explode(” “, $content));
}
函數(shù)調(diào)用方法:
<?php echo wcount(); ?>
8、禁止 WordPress 自動保存文章
要禁用 WordPress 的自動保存功能,請編輯 functions.php 文件并添加以下代碼:
function disableAutoSave(){
wp_deregister_script('autosave');
}
add_action( 'wp_print_scripts', 'disableAutoSave' );
9、告別 Pingbacks/h3>
在 phpMyAdmin 中執(zhí)行以下語句,一鍵搞定惡心的 Pingbacks 功能。
UPDATE `wp_posts` SET ping_status="closed";
10、為 WordPress 文章插入作者信息
編輯主題對應(yīng)的 functions.php 文件并粘貼以下代碼:
function get_author_bio ($content=''){
global $post;
$post_author_name=get_the_author_meta("display_name");
$post_author_description=get_the_author_meta("description");
$html="<div class='clearfix' id='about_author'>";
$html.="<img width='80' height='80' class='avatar' src='http://www.gravatar.com/avatar.php?gravatar_id=".md5(get_the_author_email()). "&default=".urlencode($GLOBALS['defaultgravatar'])."&size=80&r=PG' alt='PG'/>";
$html.="<div class='author_text'>";
$html.="<h4>Author: <span>".$post_author_name."</span></h4>";
$html.= $post_author_description."";
$html.="</div>";
$html.="<div class='clear'></div>";
$content .= $html;
return $content;
}
add_filter('the_content', 'get_author_bio');