WordPressオリジナルテーマを作ってみよう シンプルなブログサイト
4.CSSでデザインする
最後にCSSでデザインを整えます。
前章まででブログの基本構成は組み立てたので、デザインは自分好みに自由に設定しましょう。
今回は、一例として以下のようなデザインでCSSを設定してみました。
cssでデザインを指定するには、各要素にidやclassを指定していきます。
以下コードの黄色でハイライトされている部分をindex.phpに追加しましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <title><?php bloginfo( 'name' ); ?><?php wp_title(); ?></title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="http://netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.css" rel="stylesheet"> <link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>"> <?php wp_head(); ?> </head> <body <?php body_class(); ?>> <!-- ヘッダー --> <header> <div class="container"> <div id="header_left"> <h1><a href="<?php echo home_url(); ?>"><?php bloginfo( 'name' ); ?></a></h1> <p><?php bloginfo( 'description' ); ?></p> </div> <div id="header_right"> <nav><?php wp_nav_menu( 'theme_location=navigation' ); ?></nav> </div> <div class="clearbox"> </div> </header> <!-- コンテンツ(記事、記事一覧) --> <div class="container"> <div class="contents"> <!-- ■■■■■■ 記事ループ スタート ■■■■■■■ --> <?php if(have_posts()): while(have_posts()): the_post(); ?> <article <?php post_class(); ?>> <!-- 記事タイトル --> <?php if( is_single() ): ?> <h1><?php the_title(); ?></h1> <?php else: ?> <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> <?php endif; ?> <!-- 記事情報(日付、カテゴリ、コメント) --> <div class="postinfo"> <time datetime="<?php echo get_the_date( 'Y-m-d' ) ?>"> <i class="fa fa-clock-o"></i> <?php echo get_the_date(); ?> </time> <span class="postcat"> <?php the_category( ', ' ); ?> </span> </div> <!-- 記事本文 --> <?php if (is_single() ): ?> <?php the_content(); ?> <!-- 記事概略 --> <?php else: ?> <div class="row"> <div class="col one-second eyecatch"> <?php if( has_post_thumbnail() ): ?> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'large' ); ?></a> <?php endif; ?> </div> <div class="col one-second excerpt"> <?php the_excerpt(); ?> <p class="more"><a href="<?php the_permalink(); ?>">続きを読む <i class="fa fa-chevron-right"></i></a></p> </div> </div> <?php endif; ?> <!-- ページナビ --> <?php if( is_single() ): ?> <div class="pagenav"> <span class="old"> <?php previous_post_link( '%link', '<i class="fa fa-chevron-circle-left"></i> %title' ); ?> </span> <span class="new"> <?php next_post_link( '%link', '%title <i class="fa fa-chevron-circle-right"></i>' ); ?> </span> </div> <?php endif; ?> </article> <?php endwhile; endif; ?> <!-- ■■■■■■ 記事ループ エンド ■■■■■■■ --> <!-- 記事一覧ページリンク --> <?php if ( $wp_query->max_num_pages > 1): ?> <div class="pagenav"> <span class="old"> <?php next_posts_link('<i class="fa fa-chevron-circle-left"></i> 古い記事 '); ?> </span> <span class="new"> <?php previous_posts_link('<i class="fa fa-chevron-circle-right"></i> 新しい記事 '); ?> </span> </div> <?php endif; ?> </div> <!-- contents --> <div class="blogmenu"> <ul> <!-- ウィジェット --> <?php dynamic_sidebar(); ?> </ul> </div> </div> <!--- container --> <footer id="contents_footer"> <div class="container"> <div id="footer_contents"> <div id="footer_sitemap"> <?php wp_nav_menu( 'theme_location=navigation' ); ?> </div> <div id="footer_txt"> <?php bloginfo( 'name' ); ?> </div> </div> <div id="footer_copyright"> <small>© 2017 original theme</small> </div> </div> </footer> <?php wp_footer(); ?> </body> </html> |
■6行目:viewport
レスポンシブデザインに対応するため、viewportの設定をしています。
・width=divice-width:表示領域の幅を端末画面の幅に合わせます。
・initial-scale=1.0:初期のズーム倍率を1.0に設定しています
⇛コンテンツの横幅をデバイスの横幅に設定し、ページを開いた瞬間は表示倍率を1倍に設定することで、スマホサイズの画面になっても見易い画面となります。
■7行目:Font Awesome
Font Awesomeというアイコンフォントを使用するため、linkタグでスタイルシートを読み込んでいます。これでアイコンフォントが使えるようになります。
使い方は、以下のアイコン一覧で使用したいアイコンをクリックし、クラス名を確認します。そのクラス名をソースコードに指定します。
Font Awesomeアイコン一覧
今回の例では、42行目の「fa fa-clock-o」、75行目の「fa fa-chevron-circle-left」などで指定しています。
style.cssは、以下のように記載します。一応、スマホサイズの時は、2カラムから1カラムに可変するレスポンシブデザイン対応です。
参考までに活用ください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
@charset "UTF-8"; /* Theme Name: original theme step4 Theme URI: http://inomacreate.com/ Author: Kohei Inomata Description: オリジナルブログテーマ。(Step4)ブログテーマ作成のベースとしてご利用ください。 Version: 1.0 License: GNU General Public License v2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html */ * { margin: 0; padding: 0; } /* ================================================================ */ /* 共通レイアウトおよびスマートフォン向けスタイル定義 */ /* ================================================================ */ body { font-family: "游ゴシック", YuGothic, "ヒラギノ角ゴ Pro W3", "Hiragino Kaku Gothic Pro", Verdana, "メイリオ", Meiryo, Osaka, "MS Pゴシック", "MS PGothic", sans-serif; } /* スマホは、カラム横並びを解除 */ .row { max-width: 650px; margin: 0 auto; margin-bottom: 0px; } .col { float: none; width: auto; margin-left: 0; } /* float解除用 */ .clearbox { content: ""; display: block; clear: both; } /* サイト共通のコンテナ */ .container { max-width: 650px; margin-left: auto; margin-right: auto; padding-left: 15px; padding-right: 15px; } /* ============ ヘッダー部定義 =============== */ header { color: #fff; background-color: #333333; margin-bottom: 30px; } header h1 a { color: #fff; } #header_left img { max-width: 250px; width: 100%; } #header_left a { text-decoration: none; color: #fff; } /* ヘッダー左ボックス */ #header_left { width: 100%; } /* ヘッダー右ボックス */ #header_right { width: 100%; } #main_image img { max-width: 1000px; width: 100%; margin-bottom: 30px; } /* ナビゲーションメニュー */ nav ul { list-style: none; display: table; table-layout: fixed; text-align: center; width: 100%; } nav li { display: table-cell; vertical-align: middle; margin: 0; } nav li:last-child { border-right: 0; } nav li a { display: block; color: #fff; font-size: 12px; text-decoration: none; padding: 10px 0; border-bottom: solid 3px #333; transition: border-color 0.7s, color 0.7s; } nav li a:hover { border-color: #39B54A; color: #39B54A; } /* ============ メインコンテンツ部定義 =============== */ .contents { background-color: #fff; margin-bottom: 20px; } /* 記事 */ article { margin-bottom: 30px; } article p{ line-height: 2.0; } article h1 {margin: 0; font-size: 28px; font-weight: lighter; clear: both} article h1 a {color: #000000; text-decoration: none} article h2 { border-left: solid 10px #333333; padding-top: 5px; padding-bottom: 5px; padding-left: 10px; margin-bottom: 20px; margin-top: 20px; border-bottom: solid 1px #333333; } .entry-title { border: none; border-bottom: 1px solid #999999; margin-top: 0; margin-bottom: 5px; font-size: 20px; padding: 0; font-weight: lighter; } .entry-title a { color: #333333; text-decoration: none; } article h3 { border-bottom: solid 2px #a9a9a9; background-color: #f5f5f5; margin-top: 15px; margin-bottom: 15px; } /* 記事の付加情報 */ .postinfo { margin-bottom: 5px; font-size: 14px; border-bottom: 0.5px dotted #333333; } .postinfo time { margin-left: 0px; color: #888888; } .postinfo time > i { color: #888888; } .postinfo a { color: #000000; text-decoration: none; } .postcat { font-size: 13px; top: 0px; left: 0px; text-align: center; margin-left: 20px; } .postcat a { color: #888888; text-decoration: none; } .postcat > i { color: #888888; } /* 記事の付加情報 */ .excerpt p { font-size: 14px; color: #4D4D4D; } .eyecatch img { max-width: 426px; max-height: 284px; width: 100%; margin: 0 auto; } .excerpt .more a {padding: 5px 20px; border: solid 1px #cccccc; border-radius: 5px; color: #000000; text-decoration: none;} .excerpt .more a:hover {background-color: #e6cc1c} .excerpt .more i {margin-left: 10px; color: #cccccc} /* =============== フッダー部 ================== */ /* フッター */ #footer_txt { font-size: 1.4em; color: #fff; } #contents_footer { background-color: #333333; } #footer_contents { width: 100%; } #footer_sitemap { margin-bottom: 20px; } #footer_sitemap ul { list-style: none; text-align: right; width: 100%; } #footer_sitemap li { margin: 0 20px 0 auto; } #footer_sitemap li a { display: block; font-size: 12px; text-decoration: none; padding: 10px 0; color: #fff; } #footer_sitemap ul { display: table; table-layout: fixed; text-align: center; } #footer_sitemap li { display: table-cell; vertical-align: middle; margin: 0; } #footer_sitemap li a { font-size: 12px; text-decoration: none; padding: 10px 0; border-bottom: solid 3px #333; transition: border-color 0.7s, color 0.7s; } #footer_sitemap li a:hover { border-color: #39B54A; color: #39B54A; } #footer_copyright { margin-top: 10px; margin-bottom: 10px; text-align: center; color: #fff; } /* =========== サイドコンテンツ ================ */ .blogmenu ul { list-style: none; } .blogmenu .widget { margin-bottom: 30px; } .blogmenu .widgettitle { margin-bottom: 10px; font-size: 1.2em; font-weight: normal; border-left: 10px solid #B3B3B3;; border-bottom: 1px solid #B3B3B3; padding-left: 10px; } .blogmenu li a {display: block; padding: 10px 5px; color: #000; font-size: 14px; text-decoration: none} .blogmenu li a:hover { background-color: #f2f2f2; } /* ===== その他共通装飾 ===== */ /* 前後の記事へのリンク */ .pagenav a { padding: 5px 10px; border: solid 1px #cccccc; border-radius: 10px; color: #666666; font-size: 12px; text-decoration: none; } .pagenav .old a { float: left; } .pagenav .new a { float: right; } .pagenav { overflow: hidden; margin-top: 40px; margin-bottom: 40px; clear: both; } /* 本文中の画像 */ article img { border: none; max-width: 100%; height: auto} .aligncenter {display: block; margin-top: 30px; margin-bottom: 30px; margin-left: auto; margin-right: auto} .alignleft {float: left; margin-bottom: 30px; margin-right: 30px} .alignright {float: right; margin-bottom: 30px; margin-left: 30px} .wp-caption {max-width: 100%} .wp-caption.text {margin: 0; color: #666666; font-size: 14px; text-align: center} /* ================================================================ */ /* PC向け向けスタイル定義 768px〜 */ /* ================================================================ */ @media only screen and (min-width:769px){ /* PC向けは、カラムを横に整列 */ .row:after { content: ""; display: block; clear: both; } .col { float: left; margin-left: 3.2%; } .col:first-child{ margin-left: 0; } /* 各カラムの幅を%で設定 */ .one-second {width: 48.4%;} .one-third {width: 31.2%;} .two-third {width: 65.6%;} /* 共通コンテナ */ .container { max-width: 1000px; overflow: hidden; padding-left: 0px; padding-right: 0px; } .contents { width: 65%; float: left; } .blogmenu { width: 30%; float: right; } /* ============ ヘッダー部定義 =============== */ /* ヘッダー左ボックス */ #header_left { width: 40%; float: left; } /* ヘッダー右ボックス */ #header_right { width: 60%; float: right; margin-top: 50px; } } |
■404行目:メディアクエリ
・media only screen and (min-width:769px)
スクリーンサイズが768px以上の時に、404行目以降のCSSが適用されます。すなわち398行目までは、スマホ用サイト、またはスマホ、PC共通のスタイルを定義し、404行目以降はPC向けだけに適用したいスタイルを記載しています。
今回の例では、PC向けのみ2カラムに設定したいので、414行目や439行目などで、float:leftを設定して横並びにしています。
floatで要素を綺麗に整列させるポイントを以下の記事で紹介していますので、参考までにどうぞ。
CSS floatで要素を綺麗に整列させる2つのポイント
まとめ
いかがでしたか?
HTML,CSSの基本知識が多少必要ですが、WordPressのテンプレートタグの使い方さえ理解すれば結構簡単に作れます。
今回紹介した各ステップ毎のソースコードを以下からダウンロードできますので、うまく実装できなかった方やサイトのベースとして活用したい方は、ダウンロードしてみてください!
WordPressオリジナルテーマサンプル
1 file(s) 3.53 MBまた、今回紹介したWordPressのテンプレートタグは、以下WordPress Codex日本語版で使い方を調べることができますので、参考にしてみてください。
WordPress Codex日本語版
それでは!!
スポンサーリンク
コメント
こちらのソースをまるっとコピーしたのですが、なぜかウィジェットが表示されません。。。
コメントありがとうございました。
こちらで再度ソースコードを確認したところ、WordPress4.9.6でウィジットは表示されました。
確認ですが、WordPress管理画面のメニューから「外観」→「ウィジェット」に入り、サイドバー1と表示されているエリアにウィジットを登録していますでしょうか?
WordPress管理画面からのウィジット登録方法については、ブログ内であまり説明していなかったので、もしかしたら登録されていない可能性があると思いました。
申し訳ありませんが、宜しくおねがいします。