1 2 3 4 5 6 7 8 9 10 |
/* Theme Name: テーマの名前(必須) Theme URL: テーマのサイトのURI Description: テーマの説明 Author: 作者の名前 Version: テーマのバージョン Tags: テーマの特徴を表すタグ(カンマ区切り/オプション) License: テーマのライセンス License URI: テーマのライセンスのURI */ |
WordPress 任意のテンプレートパーツを読み込む
//content.php を呼び出す
1 |
<?php get_template_part( 'content' ); ?> |
// […]
ショートコードを定義する
1 2 3 4 5 6 7 8 |
<?php function testFunc() { ob_start(); echo the_title(); return ob_get_clean(); } add_shortcode('test', 'testFunc'); ?> |
カテゴリに画像を登録する Categories Images
Categories Imagesはカテゴリにアイキャッチを登録して表示したい時に便利なプラグイン。 カテゴリ […]
アイキャッチのデフォルト画像を指定するプラグイン Default featured image
アイキャッチ画像を表示するWordpressテーマを使っている場合やオリジナルテーマでアイキャッチ付きの投稿リ […]
投稿画面を旧インターフェースに戻すClassic Editor
WordPress5.0から「Gutenberg」というエディタがされました。使い慣れていないとテーマ作成や変 […]
アイキャッチ自動生成するAuto Post Thumbnail
Auto Post Thumbnailの導入 WordPress管理画面「プラグイン」→「新規追加」 「Aut […]
「Show Current Template」でどのテンプレートを使っているか確認
「Show Current Template」は サイトのツールバーに表示中のページに使われているテンプレート […]
投稿記事の順番を並び替えるWordPressプラグインIntuitive Custom Post Order
Intuitive Custom Post Order の導入 WordPress管理画面「プラグイン」→「新 […]
Intuitive Custom Post Orderで並べ替えたカテゴリの表示順で記事表示
簡易的にタグごと表示する場合
1 |
<?php $list = wp_list_categories( 'echo=0&hide_empty=0&hierarchical=1' ); ?> |
オプションを細 […]
httpからhttpsに.htaccessを使ってリダイレクトさせる
httpからhttpsへリダイレクトするには.htaccessに下記を追加 評価を引き継ぐ必要がある場合は、下 […]
WordPressの挿入画像でwidth/heightを削除
下記コードをfunctions.phpへ記述追加
1 2 3 4 5 6 7 |
// メディア追加時のwidth/height自動追加を削除 function my_remove_width_attribute( $options ) { if ( $options['tinymce'] ) { wp_enqueue_script( 'remove_width_attribute', get_template_directory_uri() . '/js/remove_width_attribute.js', array( 'jquery' ), '1.0.0', true); } } add_action( 'wp_enqueue_editor', 'my_remove_width_attribute', 10, 1 ); |
複数の投稿タイプをまとめて表示
一般的なquery_postsでのメインループの条件 [crayon-5ee29df01c2889413731 […]
投稿の日付を表示する
フォーマットを指定して投稿日を表示するコード
1 |
<?php echo get_the_date('Y年n月j日(l)'); ?> |
[…]
個別記事ページ
コード例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php get_header(); ?> <main> <?php if(have_posts()): while(have_posts()):the_post(); ?> <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> <time datetime="<?php the_time('Y-m-d'); ?>"><?php the_time('Y.m.d'); ?></time> <p><?php the_category(', '); ?></p> <p><?php the_content('Read more'); ?></p> <?php endwhile; endif; ?> <?php previous_post_link('%link','古い記事へ'); ?> <?php next_post_link('%link','新しい記事へ'); ?> </main> <?php get_sidebar(); ?> <?php get_footer(); ?> |
固定ページ
サンプルコード
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php get_header(); ?> <?php get_header(); ?> <main> <p><?php single_cat_title(); ?>カテゴリー記事の一覧です</p> <?php while ( have_posts() ) : the_post(); ?> //テンプレートがあれば <?php get_template_part( 'content', 'page' ); ?> //テンプレートがなければ <?php the_content(); ?> <?php endwhile; // end of the loop. ?> </main> <?php get_sidebar(); ?> <?php get_footer(); ?> |
カテゴリーページ
サンプルコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php get_header(); ?> <main> <p><?php single_cat_title(); ?>カテゴリー記事の一覧です</p> <?php if(have_posts()): while(have_posts()):the_post(); ?> <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> <time datetime="<?php the_time('Y-m-d'); ?>"><?php the_time('Y.m.d'); ?></time> <p><?php the_category(', '); ?></p> <p><?php the_content('Read more'); ?></p> <?php endwhile; endif; ?> <?php previous_posts_link('新しい投稿ページへ'); ?> <?php next_posts_link('古い投稿ページへ'); ?> </main> <?php get_sidebar(); ?> <?php get_footer(); ?> |
特定のカテゴリー投稿の一覧表示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php $arg = array( 'posts_per_page' => 10, // 表示する件数 'orderby' => 'date', // 日付ソート 'order' => 'DESC', // DESCで降順、ASCで昇順 'category_name' => 'wp-customize' // カテゴリーのスラッグを指定 ); $posts = get_posts( $arg ); if( $posts ): ?> <ul> <?php foreach ( $posts as $post ) : setup_postdata( $post ); ?> <li><?php the_time( 'Y.m.d' ); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> <?php endif; wp_reset_postdata(); ?> |
カスタム投稿名を出力
カスタム投稿名を出力するコード
1 |
<?php echo esc_html(get_post_type_object(get_post_type())->label); ?> |