会員専用のコンテンツや、検索に拾われないページを作りたいときに使います。
1)ベーシック認証を使用できるようにするための記述。
function basic_auth($auth_list,$realm="Restricted Area",$failed_text="認証に失敗しました"){ if (isset($_SERVER['PHP_AUTH_USER']) and isset($auth_list[$_SERVER['PHP_AUTH_USER']])){ if ($auth_list[$_SERVER['PHP_AUTH_USER']] == $_SERVER['PHP_AUTH_PW']){ return $_SERVER['PHP_AUTH_USER']; } } header('WWW-Authenticate: Basic realm="'.$realm.'"'); header('HTTP/1.0 401 Unauthorized'); header('Content-type: text/html; charset='.mb_internal_encoding()); die($failed_text); }
2)ベーシック認証をかけたいページを指定する。header.phpの先頭に記述。
固定ページの場合
<?php if(!is_home()): if(is_page('2')): //Basic認証を掛けたいページID $userArray = array( "admin" => "password" ); basic_auth($userArray); endif; endif; ?>
カテゴリの場合
<?php if(!is_home()): if(is_category('2')) : //Basic認証を掛けたいカテゴリID $userArray = array( "admin" => "password" ); basic_auth($userArray); endif; endif; ?>
投稿ページの場合
<?php if(!is_home()): if(is_single('2')) : //Basic認証を掛けたい投稿ID $userArray = array( "admin" => "password" ); basic_auth($userArray); endif; endif; ?>
!)ユーザーIDやパスワードを入力しても弾かれる場合
RewriteEngine On RewriteCond %{HTTP:Authorization} ^(.*) RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
WordPressのパーマリンク設定の記述とは別に書くと吉。(WordPressの自動アップデートで上書きされない)
アーカイブページで、タイトルや本文を抜粋して表示したいとき
タイトルの抜粋
<?php echo mb_substr(get_the_title(), 0, 40); ?>
本文の抜粋
<?php echo mb_substr(get_the_excerpt(), 0, 100); ?>
本文 文末文字指定付き
<?php echo mb_substr(strip_tags($post-> post_content),0,100) . '...'; ?>
カスタムフィールドの値の抜粋
<?php echo mb_substr((post_custom('カスタムフィールド名')),0,100); ?>
カスタムフィールドの値の抜粋(改行も反映させる場合)
<?php echo mb_substr(nl2br(post_custom('カスタムフィールド名')),0,100); ?>
トップページの新着一覧などで、各投稿のカテゴリーがひと目で分かるように、色を変えたり、画像を出し分けたい。
取得したカテゴリーにclassを付ける
<?php the_category(); ?>
の代わりに
<?php $cat = get_the_category(); $cat = $cat[0]; { echo '<span class="cat-' . $cat->category_nicename . '" >'; } ?> <?php the_category(' '); ?></span>
を使う。
これで取得されたカテゴリー名は、cat-カテゴリースラッグのclassが付くので、あとはcssで装飾。
例えばお知らせ(topics)カテゴリの場合
.cat-topics a{ display:inline-block; padding:0 20px; font-size:12px; font-weight:bold; line-height:20px; color:#fff; background:#ff7800; border-radius:4px; }
WordPressの投稿にはビジュアルエディタがついています。
HTMLがわからない人でも記事を投稿するのに便利なのですが、、、よく崩れます!
HTMLタグでレイアウトを組んだページをビジュアルエディタで編集しようとすると、タグが消える(怒)。
また、ありがた迷惑?のオートPによって、pタグに付けたstyleが意図せず効いてしまう。
これらが起こらないようにするために、そもそもの設計で、ビジュアルエディタを使用できるのは「お知らせ」や「ブログ」などのシンプルな記事に限定しておきます。
崩れてもらっては困る「固定ページ」のレイアウト。
そもそも、固定ページではビジュアルエディタは使わない。表示を消す。
//固定ページはテキストエディタのみ function disable_visual_editor_in_page(){ global $typenow; if( $typenow == 'page' ){ add_filter('user_can_richedit', 'disable_visual_editor_filter'); } } function disable_visual_editor_filter(){ return false; } add_action( 'load-post.php', 'disable_visual_editor_in_page' ); add_action( 'load-post-new.php', 'disable_visual_editor_in_page' );
<?php the_content(); ?>
のかわりに
<?php remove_filter('the_content', 'wpautop'); the_content(); add_filter('the_content', 'wpautop'); ?>
を使う。
その記事の属するカテゴリーを取得する。ループ外で使用可。
<?php $category = get_the_category(); $cat_name = $category[0]->cat_name; ?> <?php echo $cat_name; ?>
Copyright © 2012 SMILEWORKS All Rights Reserved.