WordPress Cheat Sheet ~WordPressチートシート~

2020.01.9

テンプレート制作関連

テンプレートを制作するのに最低限必要なファイルと記述

index.php

<?php get_header(); ?>

<?php get_footer(); ?>

header.php

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><?php bloginfo('name'); ?></title>
<meta name="keywords" content="<?php bloginfo('keywords'); ?>">
<meta name="description" content="<?php bloginfo('description'); ?>">
<link rel="stylesheet" href="<?php bloginfo( 'stylesheet_url' ); ?>">
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>

footer.php

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

functions.php

<?php

※phpの閉じタグは書かない

style.css

@charset "utf-8";

/*
Theme Name: テーマの名前(管理画面のテーマ選択で表示される)
*/

@import url(assets/css/pc_style.css) screen and (min-width: 769px);
@import url(assets/css/sp_style.css) screen and (max-width: 768px);

※外部CSSは適宜必要に応じてパス、ブレイクポイントを変更する

サイドバーにウィジェット機能を実装

functions.phpに下記を記述

function my_theme_widgets_init() {
  register_sidebar( array(
    'name' => 'Main Sidebar',
    'id' => 'main-sidebar',
  ) );
}
add_action( 'widgets_init', 'my_theme_widgets_init' );

テンプレートファイル内でウィジェットを表示させる場所に下記を記述

<?php if ( is_active_sidebar('main-sidebar') ) : ?>
  <ul class="menu">
    <?php dynamic_sidebar('main-sidebar'); ?>
  </ul>
<?php endif; ?>

テンプレートタグ

基本となるテンプレートタグ

サイトタイトル

<?php bloginfo('name'); ?>

サイトURL

<?php echo home_url(); ?>

テーマテンプレートURL

<?php echo get_template_directory_uri(); ?>

投稿等でつかうテンプレートタグ

記事タイトル

<?php the_title(); ?>

本文

<?php the_content(); ?>

記事のURL

<?php echo get_permalink(); ?>

記事を投稿した日付

<?php the_time('Y年m月d日(D)'); ?>

記事を更新した日付

<?php the_modified_date('Y年m月d日(D)'); ?>

よく使うプラグイン

Classic Editor

クソみたいな新しいエディタを従来のものにするプラグイン。

All-in-One WP Migration

バックアップはもちろん、ローカルで制作したWordPressをWeb上の環境に反映させるのに便利。

Password Protected

WordPressサイトにパスワードをかけることが出来るプラグイン。テストサイト等でベーシック認証代わりにつかえる。