1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

wordpress用PHPタグまとめ

Posted at

記事一覧などに使うループ処理

wordpressExample.php
<?php if (have_posts()) : ?> #もし投稿記事がある場合                        
    <?php the_title(); ?> #記事タイトルを表示する
<?php endif ?> #必ずendifで閉じる

ループ処理を追加。

wordpressExample.php
<?php if (have_posts()) : ?> 
  <?php while (have_posts()): ?> #もし投稿記事が複数ある場合
    <?php the_post(); ?> #このthe_postは必ずwhile (have_posts())とセットで使う。使わないと無限ループに
    <?php the_title(); ?> #同上
  <?php endwhile; ?> #必ずendwhileで閉じる
<?php endif ?>

上を短縮すると

wordpressExample.php
<?php if (have_posts()) : ?> 
  <?php while (have_posts()): the_post(); ?> #もし投稿記事が複数ある場合
    <?php the_title(); ?> #同上
  <?php endwhile; ?> #必ずendwhileで閉じる
<?php endif ?>

投稿日付を表示するテンプレートタグ

注意しなければならなことは、だけだと、同じ日に投稿した記事が複数あった場合、最新の投稿にしか日付が表示されない。
なので、を利用する。(もしくはget_the_date)

の引数に'Y/m/d'を追記すると、同上の場合でもきちんと表示がされる。

引数にget_option('date_format')を指定すると、wordpressの管理画面から

ブログ一覧からブログ記事ページへ

をのpost.htmlをに変更

headタグの中をheader.phpへ。footerタグの中をfooter.phpへ移動

①headタグの中身をheader.phpへ。footerタグ(jsの読み込みなど)をfooter.phpへそれぞれ移動させ、共通パーツ化する
②切り取った場所にはそれぞれを記述し、読み込めるようにする。
③haederタグのtitleの中は、ページによって変わらないといけないので

に変更。
④cssやjsなど各ファイルへのパスをに置換。
CDNの httpでもhttpsでもパスが通るように。
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/style.css">

他部分も共通パーツ化する

ヘッダーナビゲーションやフッターなどの部分も、共通パーツ化ができる。
includesという共通パーツ専用のフォルダを作成し、中にheader.phpやfooter.phpを作成。
ヘッダーナビやフッターをそれぞれコピペし、タグを使って、パスを通す

<?php get_template_part('includes/header')?>

アイキャッチ画像を設定できるようにする

アイキャッチ画像は標準では搭載されないようになっている為、設定できるようにしていく必要がある。
①functions.phpを作成し以下を記述
※ファイル名は必ずfunction"s"

functions.php
<?php
//アイキャッチ画像を設定できるように宣言。
add_action('init', function() {
    add_theme_support('post-thumbnails');
}); 

②それを出力するコードをsingle.phpに追記

single.php
<?php the_post_thumbnail(); ?>
1
5
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
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?