wp以外を触ることが多くなって、せっかく覚えたのがすっぽ抜けてる
包括的なアレ
サイト名
1 | <?php bloginfo( 'name' ); ?> |
キャッチフレーズ
1 | <?php bloginfo( 'description' ); ?> |
WEBサイトURL(末尾/あり)
1 | <?php echo esc_url( home_url( '/' ) ); ?> |
テーマディレクトリ(※親テーマ)
1 2 3 | <?php echo get_template_directory_uri(); ?> //style.css読み込み <link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/style.css"> |
ヘッダー・フッター読み込み
1 2 3 4 5 6 7 | <?php get_header(); ?> <?php get_footer(); ?> //header-aaa.php読み込み <?php get_header('aaa'); ?> //footer-bbb.php読み込み <?php get_footer('bbb'); ?> |
functions.php
アイキャッチ有効化
1 2 3 4 5 6 | function twpp_setup_theme() { add_theme_support( 'post-thumbnails' ); set_post_thumbnail_size( 600, 400, true ); } add_action( 'after_setup_theme', 'twpp_setup_theme' ); |
サムネイルサイズ指定
1 2 | add_image_size('post_120x120_thumbnail', 120, 120, true); add_image_size('post_150x150_thumbnail', 150, 150, true); |
アップロード後の指定は反映されない。再アップロードするか、それ用のプラグインを使って対処する。
親スラッグ指定有効化(.cssとかをifで振り分けるとき用)
1 2 3 4 5 6 7 | function is_parent_slug() { global $post; if ($post->post_parent) { $post_data = get_post($post->post_parent); return $post_data->post_name; } } |
活用例:ページ自体または親のスラッグが「form」だったらcss/form.css読み込み
1 2 3 | <?php if(is_page('form')||is_parent_slug()==='form'): ?> <link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/css/form.css"> <?php endif; ?> |
活用例:固定ページ「news」またはカスタム投稿記事「post_news」またはカスタム投稿アーカイブ「post_news」だったらcss/news.css読み込み
1 2 3 | <?php if(is_page('news')||is_singular('post_news')||is_post_type_archive('post_news') ): ?> <link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/css/news.css"> <?php endif; ?> |
これに限らず条件は色々指定できる。
ナビゲーション宣言
1 2 3 4 5 6 7 8 | function menu_setup() { register_nav_menus( array( 'global' => 'グローバルメニュー', 'footer' => 'フッターメニュー', )); } add_action( 'after_setup_theme', 'menu_setup' ); |
title生成(どういう作用なのかは説明してる記事を読む)
1 | add_theme_support( 'title-tag' ); |
投稿周り
記事タイトル
1 | <?php the_title(); ?> |
記事本文
1 | <?php the_content(); ?> |
記事抜粋
1 | <?php the_excerpt(); ?> |
パーマリンク
1 | <?php the_permalink(); ?> |
投稿日時
1 2 | <?php the_time(); ?> <time class="published" datetime="<?php the_time('c'); ?>"><?php the_time(get_option('date_format')); ?></time> |
カテゴリ・タグ
1 2 3 4 5 6 7 8 | <ul> <?php if(get_the_category()): ?> <li class="cat"><?php foreach((get_the_category()) as $cat){ echo $cat->cat_name . ' ';} ?></li> <?php endif; ?> <?php if(get_the_tags()): ?> <li class="tag"><?php $posttags = get_the_tags(); if ( $posttags ) {echo '<ul>';foreach ( $posttags as $tag ) { echo '<li>'.$tag->name.'</li>';}echo '</ul>';} ?></li> <?php endif; ?> </ul> |
編集ボタン(ログイン時のみ)
1 | <?php edit_post_link('この記事を編集', '<span class="edit">', '</span>'); ?> |
サムネイル
1 2 3 | <?php if(has_post_thumbnail()): ?> <?php the_post_thumbnail(); ?> <?php endif; ?> |
前の記事・次の記事(single.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <ul class="pager_list"> <?php $prevpost = get_adjacent_post(false, '', true); //前の記事 $nextpost = get_adjacent_post(false, '', false); //次の記事 if($prevpost or $nextpost): //前の記事、次の記事いずれか存在しているとき ?> <?php if ($prevpost): ?> <li class="prev"><a href="<?php echo get_permalink($prevpost->ID); ?>" title="<?php echo get_the_title($prevpost->ID); ?>"><time><?php echo get_the_time(get_option('date_format'),$prevpost->ID); ?></time><span><?php echo get_the_title($prevpost->ID); ?></span></a></li> <?php endif; ?> <?php if ($nextpost): ?> <li class="next"><a href="<?php echo get_permalink($nextpost->ID); ?>" title="<?php echo get_the_title($nextpost->ID); ?>"><time><?php echo get_the_time(get_option('date_format'),$nextpost->ID); ?></time><span><?php echo get_the_title($nextpost->ID); ?></span></a></li> <?php endif; ?> <?php endif; ?> </ul> |
sidebar読み込み
1 2 3 4 | //sidebar.php <?php get_sidebar(); ?> //sidebar-blog.php <?php get_sidebar('blog'); ?> |
カスタム投稿周り
カスタム投稿一覧+ページャー(wp_pagenavi)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | <section id="news"> <div class="list_wrap"> <div class="content"> <div class="list_set"> <ul class="news_list"> <?php $paged = get_query_var('paged')? get_query_var('paged') : 1; ?> <?php //「post_news」を10件まで表示 $myposts = new WP_Query(array( 'post_type' => 'post_news', //カスタム投稿名 'posts_per_page'=> 10, //表示件数(-1で全ての記事を表示) 'paged' => $paged )); ?> <?php if($myposts->have_posts()): while($myposts->have_posts()): $myposts->the_post(); ?> <li> <div class="inner"> <div class="ttl"><p><?php the_title();?></p></div> <div class="date"><time><?php the_time('Y/m/d'); ?></time></div> </div> <a href="<?php the_permalink(); ?>"></a> </li> <?php endwhile; ?> <?php else: ?> <?php endif; ?> <?php wp_reset_query(); ?> </ul> </div> <div class="pager"> <?php if(function_exists('wp_pagenavi')) { wp_pagenavi(array('query'=>$myposts)); } ?> </div> </div> </div> </section> |
投稿数カウント・活用
1 2 3 4 5 6 7 | <?php //「post_news」の投稿数が指定表示数(10件)よりも多くなったら記事一覧へのリンク表示 $count_post = wp_count_posts( 'post_news' ); if($count_post->publish > 10): ?> <div class="link"><a href="****">Back number</a></div> <?php endif; ?> |
応用:ターム指定したカスタム投稿一覧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php $myposts = new WP_Query(array( 'post_type' => '【カスタム投稿名】', 'posts_per_page'=> -1, 'tax_query' => array ( 'relation' => 'AND', array ( 'taxonomy' => '【カスタム投稿タクソノミー名】', 'field' => 'slug', 'terms' => array ( '【ターム】' ), 'operator' => 'IN' ) ) )); ?> |
もっと細かく指定したい場合はこちらを確認
コメント