WordPress 初心者にも分かるデフォルトテーマ twentyten の functions.php を解体して学ぶ基本設定編の続編です。この記事で解体は完了します。
一応この記事をあわせてデフォルトテーマは完全に把握できるので、是非とも前回の記事を読んだ人はこちらの記事も読んでみてください。
抜粋表示の文字数を変更する
以下のコードで抜粋表示の量を変更することができます。ただデフォルトテーマでは変更しても変化がありませんでした。以下の記事がヒントになるかもしれません。
WP Multibyte Patch で excerpt_length フィルターが未適用 – WordPress 日本語版作成チーム | Google グループ
1 2 3 4 | function twentyten_excerpt_length( $length ) { return 40; } add_filter( 'excerpt_length', 'twentyten_excerpt_length' ); |
続きを読むリンクを作る関数
この関数は続きを読むリンクを作っています。続きを読むリンクの文字列を変更したい場合はこの部分を弄ればまとめて変更することができます。
1 2 3 | function twentyten_continue_reading_link() { return ' <a href="'. get_permalink() . '">' . __( 'Continue reading <span class="meta-nav">→</span>', 'twentyten' ) . '</a>'; } |
抜粋表示の下に続きを読むリンクを付ける関数
以下のコードは続きを読むリンクを設定しています。これがなければ抜粋しか表示されず続きを読むことは出来ません。ここで先ほど紹介した続きを読むリンクを作る関数を使っています。
1 2 3 4 | function twentyten_auto_excerpt_more( $more ) { return ' …' . twentyten_continue_reading_link(); } add_filter( 'excerpt_more', 'twentyten_auto_excerpt_more' ); |
続きを読むリンクを return すれば良いです。$more に入ってくるのは抜粋データです。これを利用して何かしたい場合はこれに触ります。
抜粋表示2の下に続きを読むリンクを付ける関数
先ほどのは the_excerpt() ですが今回のは get_the_excerpt() 用の設定です。この関数の場合は return するのは抜粋表示の値です。そのため $output で取得した抜粋文字列の最後に続きを読むリンクを付けてあげます。
1 2 3 4 5 6 7 | function twentyten_custom_excerpt_more( $output ) { if ( has_excerpt() && ! is_attachment() ) { $output .= twentyten_continue_reading_link(); } return $output; } add_filter( 'get_the_excerpt', 'twentyten_custom_excerpt_more' ); |
ギャラリーのスタイルシートを削除する
以下のコードはギャラリーを出力する際に出てくる CSS の削除コードです。1行目の ‘__return_false’ を return と置き換えれば消えます。ただし WordPress 3.1 よりバージョンがフリものはこれでは消えませんので twentyten_remove_gallery_css() で文字列置換をして消しています。
1 2 3 4 5 6 7 8 | add_filter( 'use_default_gallery_style', '__return_false' ); function twentyten_remove_gallery_css( $css ) { return preg_replace( "#<style type='text/css'>(.*?)</style>#s", '', $css ); } // Backwards compatibility with WordPress 3.0. if ( version_compare( $GLOBALS['wp_version'], '3.1', '<' ) ) add_filter( 'gallery_style', 'twentyten_remove_gallery_css' ); |
コメントの出力関数を定義する
340行から381行までで定義されている twentyten_comment() はマークアップ方法を変更する関数です。以下の関数のコールバックとして呼び出されています。内容としてはマークアップの方法を変更している程度で大したことはしていないので自分で今まで使っていたものがあればそれに書き換えても構いません。
1 | wp_list_comments(array('callback' => 'twentyten_comment')); |
ウィジェットを表示する方法
サイドバーを管理画面から弄ることができる機能です。基本機能で説明していたメニューと似たような感じですがこちらはさらに応用版のような機能になっています。
まず functions.php でウィジェットを設定する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function twentyten_widgets_init() { register_sidebar( array( 'name' => 'サイドバーのウィジェットエリア', 'id' => 'sidebar-widget-area', 'description' => 'ここはサイドバーのウィジェットエリアです', 'before_widget' => '<li id="%1$s" class="widget-container %2$s">', 'after_widget' => '</li>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h2>', ) ); register_sidebar( array( 'name' => 'フッターのウィジェットエリア', 'id' => 'footer-widget-area', 'description' => 'ここはフッターのウィジェットエリアです', 'before_widget' => '<li id="%1$s" class="widget-container %2$s">', 'after_widget' => '</li>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h2>', ) ); } add_action('widgets_init', 'twentyten_widgets_init'); |
設定したウィジェットを各場所で読み込む
読み込み方はいろいろあります。以下は読み込みつつ条件分岐するパターン。
1 2 3 | <?php if(!dynamic_sidebar('sidebar-widget-area')): ?> ウィジェットに何も含まれていない場合ここが表示されます。 <?php endif; ?> |
以下は条件分岐しつつ読み込むパターン。
1 2 3 4 | <?php if(is_active_sidebar('footer-widget-area')): ?> 存在する場合にここが表示されウィジェットも表示される。 <?php dynamic_sidebar( 'footer-widget-area' ); ?> <?php endif; ?> |
以下はウィジェットがひとつも設定されてなければデフォルトを表示するパターン。
1 2 3 4 5 6 7 8 9 10 | <?php if( !is_active_sidebar('sidebar-widget-area') && !is_active_sidebar('footer-widget-area') ) { ?> ウィジェットが全て設定されていない場合にここを表示する。 <?php endif; ?> |
新着コメントと一緒に出てくるスタイルを削除するか
以下のコードを修正すればスタイルを除去することができます。
以下のコードではスタイルを出力となっていますが、’__return_false’ を false にすることで出力を停止することができます。
1 2 3 4 | function twentyten_remove_recent_comments_style() { add_filter('show_recent_comments_widget_style', '__return_false'); } add_action('widgets_init', 'twentyten_remove_recent_comments_style'); |
ちなみに出力されるスタイルは以下のようになっています。
1 2 3 4 5 6 7 | <style type="text/css"> .recentcomments a { display:inline !important; padding:0 !important; margin:0 !important; } </style> |
現在の記事と著者のメタ情報を HTML とまとめて出力する関数
以下の関数はただまとめて情報を出力できるだけの便利関数です。
もし表示を変更したければこの部分の HTML を弄るだけでもいけます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function twentyten_posted_on() { printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep">by</span> %3$s', 'twentyten' ), 'meta-prep meta-prep-author', sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>', get_permalink(), esc_attr( get_the_time() ), get_the_date() ), sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>', get_author_posts_url( get_the_author_meta( 'ID' ) ), sprintf( esc_attr__( 'View all posts by %s', 'twentyten' ), get_the_author() ), get_the_author() ) ); } |
現在の記事のタグ、カテゴリやパーマリンクをまとめて出力する関数
以下の関数はただまとめて情報を出力できるだけの便利関数です。
もし表示を変更したければこの部分の HTML を弄るだけでもいけます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function twentyten_posted_in() { // Retrieves tag list of current post, separated by commas. $tag_list = get_the_tag_list( '', ', ' ); if ( $tag_list ) { $posted_in = __( 'This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' ); } elseif ( is_object_in_taxonomy( get_post_type(), 'category' ) ) { $posted_in = __( 'This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' ); } else { $posted_in = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'twentyten' ); } // Prints the string, replacing the placeholders. printf( $posted_in, get_the_category_list( ', ' ), $tag_list, get_permalink(), the_title_attribute( 'echo=0' ) ); } |
コメント