WordPress週間の最終日です。今日は、
地味で、あまり知られていないけど、
知っておくと割と便利なカスタマイズ
Tips集。CMSとして使う際は結構頻度
が高くなります。覚えておいて損は
しないと思います。
当サイトのWordPress週間、今日は最終日です。先日WordPressのカスタマイズに便利なスニペット集をリリースしました。その宣伝も兼ねて。
WordPressスニペット
今日は、地味だけど覚えておくと割と便利なWordPressカスタマイズTipsいろいろです。動作テストはWordPress Ver.3.3.1で行いました。順不同です。
1つのカスタムフィールドで複数の値を登録→実装
カスタムフィールドは沢山あると使いにくくなります。シンプルなものなら1つにまとめてしまいましょう。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
if( post_custom('foo') ) {
echo "<ul>\n";
$items = explode("," , post_custom('foo'));
//コンマで区切ったアイテムを配列
foreach($items as $value) {
echo "<li>".$value."</li>\n";
}
echo "</ul>\n";
}
?>
/*fooという名前のカスタムフィールドで、「,」で区切られた値をリストとして出力する*/
<?php
if( post_custom('foo') ) {
echo "<ul>\n";
$items = explode("," , post_custom('foo'));
//コンマで区切ったアイテムを配列
foreach($items as $value) {
echo "<li>".$value."</li>\n";
}
echo "</ul>\n";
}
?>
/*fooという名前のカスタムフィールドで、「,」で区切られた値をリストとして出力する*/
一つの「名前」で複数の値を登録するには、値の中でコンマで区切って(犬,猫,鳥という感じで書く)配列に放り込んでforeachで回します。サンプルコードではリストにしています。
via:WordPress ひとつのカスタムフィールドで複数項目入力/表示する
the_content内の全てのa要素に任意の属性を追加
記事内のアンカータグ全てにtarget=”_blank”やrel=”lightbox”みたいな属性を与えます。これに限らず、記事内(the_content)に何か加える為のコードとして参考になるかと思います。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
function autoblank($text) {
$return = str_replace('<a', '<a target="_blank"', $text);
return $return;
}
add_filter('the_content', 'autoblank');
?>
/*「<a」という文字列を「<a target="_blank"」に置き換える。変換する文字列はなんでも良い*/
<?php
function autoblank($text) {
$return = str_replace('<a', '<a target="_blank"', $text);
return $return;
}
add_filter('the_content', 'autoblank');
?>
/*「<a」という文字列を「<a target="_blank"」に置き換える。変換する文字列はなんでも良い*/
via:
Add target=”_blank” on all link
プラグイン不要でGoogle翻訳による多言語化
多言語化したいけど、予算無いからGoogle翻訳出来る様にしてよ、みたいな案件・・・が無いとは言い切れません。
Google翻訳はエンコードされたURLが必要なので通常のテンプレートタグでは実装できませんので、以下のようなサーバー変数($_SERVER)を用いてエンコードします。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul id="translations">
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Cde">Deutsch</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Ces">Espanol</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Cfr">Francais</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Cit">Italiano</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Cpt">Portugues</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Car">Arabic</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Cen">English</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Cko">Korean</a></li>
</ul>
<ul id="translations">
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Cde">Deutsch</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Ces">Espanol</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Cfr">Francais</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Cit">Italiano</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Cpt">Portugues</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Car">Arabic</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Cen">English</a></li>
<li><a rel="nofollow" href="http://translate.google.com/translate?u=<?php echo urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>&langpair=ja%7Cko">Korean</a></li>
</ul>
テキストリンクにしていますが、国旗アイコンのほうが分かりやすいですね。
via:
Valid, SEO-Friendly Post Translation Links
wp_list_categoriesで生成されるカテゴリのリンクリストを2列に
カテゴリが増えると縦長になりますが、増やさざるを得ない、などの場合は2列にしてコンパクト化しては如何でしょう。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
$cats = explode("<br />",wp_list_categories('title_li=&echo=0&depth=1&style=none'));
$cat_n = count($cats) - 1;
for ($i=0;$i<$cat_n;$i++):
if ($i<$cat_n/2):
$cat_left = $cat_left.'<li>'.$cats[$i].'</li>';
elseif ($i>=$cat_n/2):
$cat_right = $cat_right.'<li>'.$cats[$i].'</li>';
endif;
endfor;
?>
<ul class="left">
<?php echo $cat_left;?>
</ul>
<ul class="right">
<?php echo $cat_right;?>
</ul>
/* 2つに分割したので、あとはcssで2列にする */
.left{float:left;}
.right{float:right;}
<?php
$cats = explode("<br />",wp_list_categories('title_li=&echo=0&depth=1&style=none'));
$cat_n = count($cats) - 1;
for ($i=0;$i<$cat_n;$i++):
if ($i<$cat_n/2):
$cat_left = $cat_left.'<li>'.$cats[$i].'</li>';
elseif ($i>=$cat_n/2):
$cat_right = $cat_right.'<li>'.$cats[$i].'</li>';
endif;
endfor;
?>
<ul class="left">
<?php echo $cat_left;?>
</ul>
<ul class="right">
<?php echo $cat_right;?>
</ul>
/* 2つに分割したので、あとはcssで2列にする */
.left{float:left;}
.right{float:right;}
リストを作って数を数え、2つに分けて、それぞれ交互に別のul要素に放り込みます。ul二つでカテゴリリストを作成します。あとはCSSでスタイルすれば良いですね。
via:
List cats two columns
複数の異なるカスタム投稿タイプに共通のカスタムタクソノミー
カスタム投稿タイプは複数使いたいけど、与えるタクソノミー(カテゴリとかタグなどの分類法)は1つに共通させたい、という場合はカスタムタクソノミーを与える際にcom_categoryで配列します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
register_taxonomy(
'com_category', array('foo', 'bar','hoge','fuga'),array(
'label' => '共通カテゴリ',
'hierarchical' => true,
'show_ui' => true,
'query_var' => true,
));
?>
/*'com_category', array('投稿タイプA', '投稿タイプB','投稿タイプC','投稿タイプD')*/
<?php
register_taxonomy(
'com_category', array('foo', 'bar','hoge','fuga'),array(
'label' => '共通カテゴリ',
'hierarchical' => true,
'show_ui' => true,
'query_var' => true,
));
?>
/*'com_category', array('投稿タイプA', '投稿タイプB','投稿タイプC','投稿タイプD')*/
via:
複数のカスタム投稿タイプに共通のカテゴリ
テーマファイルで記事タイトルを文字数制限
記事タイトルの文字数をテーマファイル内で制御します。省略だけならCSSでも出来ますが、これなら文字数を指定できますし、クロスブラウザに対応できます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (strlen($post->post_title) > 30) {
echo mb_substr(the_title($before = '', $after = '', FALSE), 0, 30) . '...'; } else {
the_title();
} ?>
/*上記は30文字以降「...」で省略される*/
<?php if (strlen($post->post_title) > 30) {
echo mb_substr(the_title($before = '', $after = '', FALSE), 0, 30) . '...'; } else {
the_title();
} ?>
/*上記は30文字以降「...」で省略される*/
via:
How to limit character on title
記事へのコメント送信後にサンキューページにリダイレクト
ユーザーがコメント送信したら任意のページにリダイレクトさせます。用途が思いつかない、という方も多いかもしれませんが、コメントの用途は「コメント」のみに限りません。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
add_filter('comment_post_redirect', 'redirect_after_comment');
function redirect_after_comment(){
wp_redirect('http://example.com/thankyou.html');
exit();
}
?>
<?php
add_filter('comment_post_redirect', 'redirect_after_comment');
function redirect_after_comment(){
wp_redirect('http://example.com/thankyou.html');
exit();
}
?>
via:
Redirect commenter to thank you post or page
ループ外でカスタムフィールドの値を取得
WordPressの進化で様々な機能が加わってきましたが、カスタムフィールドは変わらず利便性に長けている事は言うまでもありません。なので、ループ外でも値を取得出来る方法を知っておくといいと思います。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
global $wp_query;
$postID = $wp_query->post->ID;
echo get_post_meta($postID, 'カスタムフィールド名', true);
?>
<?php
global $wp_query;
$postID = $wp_query->post->ID;
echo get_post_meta($postID, 'カスタムフィールド名', true);
?>
via:
Custom field outside loop
登録した全てのカテゴリーの数を取得
ブログではなく、Webサービス等にWordPressを使用する事もあるかと思います。その際、カテゴリ数を取得出来ると便利です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
$numcats = wp_count_terms('category');
echo $numcats;
?>
<?php
$numcats = wp_count_terms('category');
echo $numcats;
?>
via:
Total number of categories
登録した全てのタグの数を取得
同上です。全てのタグ数を取得します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
$numtags = wp_count_terms('post_tag');
echo $numtags;
?>
<?php
$numtags = wp_count_terms('post_tag');
echo $numtags;
?>
特定の日~特定の日までに投稿された記事を羅列するループ
日付で記事を限定したループの作り方です。これを使えば、例えば学校のサイトなどで「2010年4月1日~2011年3月31日までの生徒の投稿記事」とかを羅列させる事が出来ます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
function filter_where($where = '') {
$where .= " AND post_date >= '2011-05-01' AND post_date <= '2012-05-01'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>
<?php if ( have_posts() ) : ?>
<?php
function filter_where($where = '') {
$where .= " AND post_date >= '2011-05-01' AND post_date <= '2012-05-01'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>
<?php if ( have_posts() ) : ?>
via:
Get posts published between two particular dates
今後とも宜しくお願い致します
と言うわけで、3回にわたってWordPressスニペット を宣伝させて頂きました。今後も更新してまいりますのでどうぞ宜しくお願い致します。