【WordPress ソースコード】入門編:プロフィールサイト

目次

テーマディレクトリ構成

    
~/wp-content/themes/profile
 ├─img
 │  ├─favicon.ico
 │  ├─logo.svg
 │  ├─about.jpg
 │  └─mainvisual.jpg
 │
 ├─css
 │  ├─top.css
 │  └─single.css
 │
 ├─header.php
 ├─footer.php
 ├─index.php
 ├─single.php
 ├─functions.php
 └─style.css
    
  

header.php

別タブで開く
header.php

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?php echo bloginfo('name'); ?></title>
  <meta name="description" content="<?php bloginfo('description'); ?>">
  <link rel="icon" href="<?php echo esc_url(get_theme_file_uri('img/favicon.ico')); ?>">

  <?php wp_head(); ?>
</head>

<body>
  <header id="header" class="wrapper">
    <?php $tag = (is_home() || is_front_page()) ? 'h1' : 'div'; ?>
    <<?php echo $tag; ?> class="site-title">
      <a href="<?php echo esc_url(home_url()); ?>">
        <img src="<?php echo esc_url(get_theme_file_uri('img/logo.svg')); ?>" alt="Profile">
      </a>
    </<?php echo $tag; ?>>

    <nav>
      <ul>
        <li><a href="<?php echo esc_url(home_url('/#about')); ?>">About</a></li>
        <li><a href="<?php echo esc_url(home_url('/#bicycle')); ?>">Bicycle</a></li>
      </ul>
    </nav>
  </header>

別タブで開く
footer.php

  <footer id="footer">
    <p>&copy; 2020 Profile</p>
  </footer>

  <?php wp_footer(); ?>
</body>
</html>

index.php

別タブで開く
index.php

<?php get_header(); ?>

<main>
  <div id="mainvisual">
    <img src="<?php echo esc_url(get_theme_file_uri('img/mainvisual.jpg')); ?>" alt="テキストテキストテキスト">
  </div>

  <section id="about" class="wrapper">
    <h2 class="section-title">About</h2>
    <div class="content">
      <img src="<?php echo esc_url(get_theme_file_uri('img/about.jpg')); ?>" alt="テキストテキストテキスト">
      <div class="text">
        <h3 class="content-title">KAKERU MIYAICHI</h3>
        <p>
          テキストテキストテキストテキストテキストテキストテキスト<br>
          テキストテキストテキストテキストテキストテキストテキスト<br>
          テキストテキストテキストテキストテキストテキストテキスト
        </p>
      </div>
    </div>
  </section>

  <section id="bicycle" class="wrapper">
    <h2 class="section-title">Bicycle</h2>
    <?php if (have_posts()): ?>
      <ul>
        <?php while (have_posts()):the_post(); ?>
          <li>
            <a href="<?php the_permalink(); ?>">
              <img src="<?php the_post_thumbnail_url('full'); ?>" alt="">
              <h3 class="content-title"><?php the_title(); ?></h3>
              <p><?php echo wp_trim_words(get_the_content(), 30, '…'); ?></p>
            </a>
          </li>
        <?php endwhile; ?>
      </ul>
    <?php endif; ?>
  </section>
</main>

<?php get_footer(); ?>

single.php

別タブで開く
single.php

<?php get_header(); ?>

<main class="wrapper">
  <article>
    <div id="mainvisual">
      <img src="<?php the_post_thumbnail_url('full'); ?>" alt="">
    </div>

    <h1 class="title"><?php the_title(); ?></h1>

    <div class="content">
      <?php the_content(); ?>
    </div>
  </article>

  <a href="<?php echo esc_url(home_url()); ?>">トップに戻る</a>
</main>

<?php get_footer(); ?>

functions.php

別タブで開く
functions.php

<?php

/**************************************************
CSSファイルの読み込み
**************************************************/
function my_enqueue_styles() {
  wp_enqueue_style('ress', '//unpkg.com/ress/dist/ress.min.css', array(), false, 'all');
  wp_enqueue_style('style', get_stylesheet_uri(), array('ress'), false, 'all');

  if(is_home() || is_front_page()) {
    wp_enqueue_style('top', get_theme_file_uri('css/top.css'), array('ress', 'style'), false, 'all');

  } elseif(is_single()) {
    wp_enqueue_style('single', get_theme_file_uri('css/single.css'), array('ress', 'style'), false, 'all');
  }
}
add_action('wp_enqueue_scripts', 'my_enqueue_styles');

/**************************************************
アイキャッチを有効化
**************************************************/
add_theme_support('post-thumbnails');

style.css

別タブで開く
style.css

@charset "UTF-8";
/*
Theme Name: profile
*/
html {
  font-size: 100%;
}
body {
  color: #383e45;
  font-size: 0.9rem;
}
a {
  color: #383e45;
  text-decoration: none;
}
img {
  max-width: 100%;
}
li {
  list-style: none;
}

.site-title {
  width: 120px;
  line-height: 1px;
  padding: 10px 0;
}
.site-title a {
  display: block;
}
.section-title {
  display: inline-block;
  font-size: 2rem;
  text-align: center;
  margin-bottom: 60px;
  border-bottom: solid 1px #383e45;
}
.content-title {
  font-size: 1rem;
  margin: 10px 0;
}
.wrapper {
  max-width: 960px;
  margin: 0 auto 100px auto;
  padding: 0 4%;
  text-align: center;
}

/*-------------------------------------------
ヘッダー
-------------------------------------------*/
#header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 auto;
}
#header ul {
  display: flex;
  padding: 10px 0;
}
#header li {
  margin-left: 30px;
}
#header li a {
  color: #24292e;
}
#header li a:hover {
  opacity: 0.7;
}

/*-------------------------------------------
footer
-------------------------------------------*/
#footer {
  font-size: 0.5rem;
  padding: 10px 0;
  text-align: center;
}

top.css

別タブで開く
top.css

@charset "UTF-8";

/*-------------------------------------------
Mainvisual
-------------------------------------------*/
#mainvisual {
  margin-bottom: 80px;
}
#mainvisual img {
  width: 100%;
  max-width: 1920px;
  height: 600px;
  object-fit: cover;
}

/*-------------------------------------------
About
-------------------------------------------*/
#about .content {
  display: flex;
  justify-content: center;
  align-items: center;
}
#about img {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  margin-right: 30px;
}
#about .text {
  text-align: left;
}

/*-------------------------------------------
Bicycle
-------------------------------------------*/
#bicycle ul {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
#bicycle li {
  width: 32%;
  margin-bottom: 40px;
}

/*-------------------------------------------
SP
-------------------------------------------*/
@media screen and (max-width: 600px) {
  /*-------------------------------------------
  Mainvisual
  -------------------------------------------*/
  #mainvisual img {
    height: calc(100vh - 60px);
  }

  /*-------------------------------------------
  About
  -------------------------------------------*/
  #about .content {
    flex-direction: column;
  }
  #about img {
    margin-right: 0;
  }

  /*-------------------------------------------
  Bicycle
  -------------------------------------------*/
  #bicycle ul {
    flex-direction: column;
  }
  #bicycle li {
    width: 100%;
  }
}

single.css

別タブで開く
single.css

@charset "UTF-8";

#mainvisual {
  margin-bottom: 30px;
}
.title {
  text-align: left;
  padding: 0 30px;
  margin-bottom: 30px;
}
.content {
  padding: 0 30px;
  margin-bottom: 60px;
  text-align: left;
}
.content p {
  line-height: 1.7;
  margin-bottom: 20px;
}

/*-------------------------------------------
SP
-------------------------------------------*/
@media screen and (max-width: 600px) {
  .title {
    padding: 0;
  }
  .content {
    padding: 0;
  }
}