LoginSignup
1
3

More than 1 year has passed since last update.

WordPressの基礎

Posted at

WordPress

WordPress Codex 日本語版

【最重要】テンプレートタグ・関数

テンプレートタグ

  • ワードプレス固有のphpタグのこと

wp_head,wp_footer

  • それぞれheadの締めタグ、bodeの締めタグの直前に記述しなけらばならない

必須

~~~

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

~~~

<?php wp_footer(); ?>
</body>
  • wordpressのテーマとして認識してもらうために必須のテンプレートタグ

分岐、ループ系

<?php if (have_posts()) : ?>
   <?php while (have_posts()): the_post(); ?>
       <?php the_title(); ?>
       <?php the_date(); ?>
  • have_postsはルwordpressクエリでループ可能なものを返す
    • 投稿があるかどうかをチェックしている
  • the_postはhave_postを一つ進める
    • 厳密には「ループを次の投稿へ進める」
  • the_titleは投稿タイトル
  • the_dateは投稿日時(形式はダッシュボードの設定で変えれる)
    • ただ同じ日付の投稿は最初の一件だけしか日付が表示されない
    • 同じ日付でも表示したい場合は the_time()を使用する↓
    • <?php the_time(get_option('date_format')); ?>

[エラー] the_title();が表示されない

  • この記事で解決

で記事のタイトルが表示されない』

  • 設定→表示設定→フロントページの表示で、固定ページから最新の投稿に変更する
  • 自分のケースは元々なってたけど、もう一回同じことをして変更保存したら治った

if分岐

<?php if (have_posts()) : ?>
~~~
<?php else : ?>
~~~
<?php endif; ?>

Whileループ

<?php while (have_posts()): the_post(); ?>
 <?php the_title(); ?>
 <?php the_date(); ?>
<?php endwhile; ?>
  • 各投稿ごとのタイトルと日付を、投稿してる分だけ繰り返している。

抜粋

<h3 class="post-subtitle">
      <?php the_excerpt(); ?>
      </h3>
  • 記事の抜粋約200文字(HTMLタグ(太字の修飾とか)と画像は取り除かれる)くらいを表示する
  • 投稿の右側の蘭から抜粋を選択すると、抜粋だけを入力できる項目がある。これを使うべし

著者名

<p class="post-meta">
          Posted by
    <?php the_author(); ?>
  • ダッシュボードのユーザー→ユーザー一覧でその設定を変更することができる

個別投稿ページの作成

<div class="post-preview">
    <a href="<?php the_permalink(); ?>">

パーツファイル(共通部分をまとめておく)

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

~~~
<body>

~~~

<?php get_footer(); ?>
<?php get_sidebar(); ?>
</body>
  • header.php(htmlのheadタグの中身だけ)
  • footer.php(bodyタグ終盤のコアの部分からwp_footerまで)
  • sideber.php(使わないならなくても良い)

header,footerでのテンプレートファイルのパス取得


<link href="<?php echo get_template_directory_uri(); ?>/css/styles.css" rel="stylesheet" />

<!-- google fontsとか外部のサイトからとってきてる場合は、先頭の通信プロトコルを記載しないでおく -->
<link href="//fonts.google....."/>

  • これで相対パス的な運用が可能となる

上記以外でのテンプレートファイルの取得

<?php get_template_part("includes/header"); ?>
  • 共通パーツ化したいものは、includesフォルダのheader.phpに移設してある

投稿の詳細ページに各情報を出力する

  1. もし投稿があったら<?php if (have_posts()): ?>
  2. 投稿の分だけ(確定で1つ)<?php while (have_posts()) : the_post(); ?>
    1. タイトル<h1><?php the_title(); ?></h1>
    2. 筆者<?php the_author(); ?>
    3. 日付<?php the_date(); ?>
    4. 内容を出力する<?php the_content(); ?>
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3