テーマ Luxeritasでfooter専用の入力欄を追加
この記事は約 6 分で読めます。
Luxeritasが
3.3.5にバージョンアップされ、
addhead 専用の入力欄が
追加されました。
今までは
ヘッダーを追加したい場合は、
記事投稿(編集)画面で、
カスタムフィールドに
addhead という名前を追加し、
値の部分に CSS や Javascript を書くことで、
ヘッダーを追加することもできます。
でしたが、
投稿画面に
追加ヘッダーという
フィールドができました。
大変便利になり、
ありがとうございます。
同じように
投稿画面にヘッダーフィールドを
追加できる方法を紹介した
記事を発見したので、
この記事の
ヘッダーフィールドを
フッターに変更してみましょう。
上記では、
CSSとJavaScriptを
記事ごとに個別に
ヘッダーに追加するフィールドが
生成されます。
この
JavaScriptを
ヘッダーに追加
できるようにするソースを
フッターに変更します。
カスタマイズは
以下の2点。
1) wp_head を wp_footerに
add_action( 'wp_head','insert_custom_js' );
↓
add_action( 'wp_footer','insert_custom_js' );
2) <script type="text/javascript"></script>を消す
<script type="text/javascript"></script>と
書かれていると、
CDN経由など外部のjsを読み込む際に
困るので記述を消します。
echo '<script type="text/javascript">' . get_post_meta(get_the_ID(), '_custom_js', true) . '</script>';
↓
echo get_post_meta(get_the_ID(), '_custom_js', true);
カスタマイズしたものが
以下になります。
下記をfunctions.phpに記述します。
add_action( 'admin_menu', 'custom_js_hooks' );
add_action( 'save_post', 'save_custom_js' );
add_action( 'wp_footer','insert_custom_js' );
function custom_js_hooks() {
add_meta_box( 'custom_js', '追加フッター', 'custom_js_input', 'post', 'normal', 'high' );
add_meta_box( 'custom_js', '追加フッター', 'custom_js_input', 'page', 'normal', 'high' );
}
function custom_js_input() {
global $post;
echo '<input type="hidden" name="custom_js_noncename" id="custom_js_noncename" value="'.wp_create_nonce('custom-js').'" />';
echo '<textarea name="custom_js" id="custom_js" rows="5" cols="30" style="width:100%;">'.get_post_meta($post->ID,'_custom_js',true).'</textarea>';
}
function save_custom_js($post_id) {
if (!wp_verify_nonce($_POST['custom_js_noncename'], 'custom-js')) return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
$custom_js = $_POST['custom_js'];
update_post_meta($post_id, '_custom_js', $custom_js);
}
function insert_custom_js() {
if ( is_page() || is_single() ) {
if ( have_posts() ) : while ( have_posts() ) : the_post();
echo get_post_meta(get_the_ID(), '_custom_js', true);
endwhile; endif;
rewind_posts();
}
}
投稿画面に
「追加フッター」が追加されました。
「追加ヘッダー」は
Luxeritas オリジナルです。
以下の方法でも
bodyの終了タグ手前に
カスタムフィールドを組み込む事が
できます。
CSSをフッターに追加する事は
少ないと思うのでパスします。
どうしても
フッターにCSSを追加する
フィールドが欲しい方は
上記を参考に
カスタマイズしてみてください。
要領は同じです。
カルーセルスライダーの
自動再生が効かない不都合の修正。
確かにスライダーの
自動再生が効かないのは
気になっていました。
公式ブログからお借りしております。
貴重なお時間を割き、
最後まで
ご高覧いただきまして
有難うございました
ディスカッション
コメント一覧
まだ、コメントがありません