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

目次

テーマディレクトリ構成

    
profile
 ├─img
 │  ├─favicon.ico
 │  ├─logo.svg
 │  ├─about.jpg
 │  └─mainvisual.jpg
 │
 ├─footer.php
 ├─functions.php
 ├─header.php
 ├─index.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="shortcut icon" href="<?php echo esc_url(get_template_directory_uri()); ?>/img/favicon.ico">

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

<body>
  <header id="header" class="wrapper">
    <h1 class="site-title">
      <a href="<?php echo esc_url(home_url()); ?>">
        <img src="<?php echo esc_url(get_template_directory_uri()); ?>/img/logo.svg" alt="Profile">
      </a>
    </h1>

    <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>

index.php

別タブで開く
index.php

<?php get_header(); ?>

<main>
  <div id="mainvisual">
    <img src="<?php echo esc_url(get_template_directory_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_template_directory_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>
            <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>
          </li>
        <?php endwhile; ?>
      </ul>
    <?php endif; ?>
  </section>
</main>

<?php get_footer(); ?>

別タブで開く
footer.php

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

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

functions.php

別タブで開く
functions.php

<?php

/**************************************************
CSSファイルの読み込み
**************************************************/
function my_enqueue_styles() {
  wp_register_style('ress', '//unpkg.com/ress/dist/ress.min.css', array(), false, 'all');
  wp_register_style('style', get_stylesheet_uri(), array('ress'), false, 'all');
  wp_enqueue_style('style');
}
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;
}

/*-------------------------------------------
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;
  justify-content: space-between;
}
#bicycle li {
  width: 32%;
}

/*-------------------------------------------
投稿ページ
-------------------------------------------*/
article .title {
  text-align: left;
  padding: 0 30px;
  margin-bottom: 30px;
}
article .mainvisual {
  margin-bottom: 30px;
}
article .content {
  padding: 0 30px;
  text-align: left;
}
article .content p {
  line-height: 1.7;
  margin-bottom: 20px;
}

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

/*-------------------------------------------
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%;
    margin-bottom: 30px;
  }

  /*-------------------------------------------
  投稿ページ
  -------------------------------------------*/
  article .title {
    padding: 0;
  }
  article .content {
    padding: 0;
  }
}