<?php if ( is_home() || is_front_page() ) : ?>
フロントページ
<?php else : ?>
ここにトップページ意外に表示したいほにゃららを記載
<?php endif; ?>
<?php if(is_category()): ?>
条件式1
<?php elseif(is_month()): ?>
条件式2
<?php elseif(is_home()): ?>
条件式3
<?php else: ?>
条件式1,2,3以外のすべて
<?php endif; ?>
【特定のカテゴリーの場合:単独】
<?php if ( in_category(array('news')) ) : ?>
<h1 class="title_sub">お知らせ</h1>
<?php else://通常ページの場合 ?>
<h1 class="title_sub">その他</h1>
<?php endif; ?>
【特定のカテゴリーの場合:複数】スラッグを指定する
<?php if ( in_category(array('design','life','html')) ) ://特定のカテゴリーの場合 ?>
<link rel="stylesheet" href="****/special.css" type="text/css" />
<?php else://通常ページの場合 ?>
<link rel="stylesheet" href="****/style.css" type="text/css" />
<?php endif; ?>
| 固定ページ | is_page() <?php if ( is_page(5) ) : ?> |
|---|---|
| 投稿記事ページの場合 | is_single() 複数:<?php if(is_single(array(2, 5, 11, 21))) :> |
| 投稿ページの場合 | is_home() ブログページとか。投稿ページがTOPページ(フロントページの場合) is_home() && is_front_page() |
| フロントページの場合 | is_front_page() |
| メインページを表示する場合は | is_home() |
| アーカイブページ(一覧)の場合 | is_archive() |
| カテゴリーページの場合 | is_category() |
| カテゴリーページの場合 | is_category() 特定のカテゴリID is_category('4') |
| カテゴリーページの中の記事 | in_category('5') 記事のIDを指定する |
| 検索結果のページを表示する場合は | is_search() |
| 404(Not Found)のページを表示する場合は | is_404(): |
カテゴリのぺージ毎にPHPファイルを入れ替える。
空の category.php ファイルを作り、下記を記述するとスラッグ別に専用のファイルを参照する。
<?php
if (is_category('スラッグ')) {
include(TEMPLATEPATH . '/archive-aaa.php');
} else if (is_category('スラッグ')) {
include(TEMPLATEPATH . '/archive-bbb.php');
} else if (is_category('スラッグ')) {
include(TEMPLATEPATH . '/archive-ccc.php');
} else if (is_category('スラッグ')) {
include(TEMPLATEPATH . '/archive-ddd.php');
} else {
include(TEMPLATEPATH . '/archive.php');
}
?>
<?php query_posts('showposts=9&cat=5'); while(have_posts()) : the_post(); ?>
<h1><a href="<?php the_permalink();?>"><?php the_title();?></a></h1>
<?php the_content();?>
<?php endwhile;?>
showposts=9 (表示件数) cat=5(カテゴリのID)
一部のカテゴリーを除外したい場合は「-」 マイナスを付けると除外される。
<?php query_posts('showposts=9&cat=-5');
カテゴリIDを調べたい時は、「投稿」「カテゴリー」で一覧を表示して、タイトルにマウスを合わせると、下に tag_ID が表示される。
<?php query_posts("category_name=news&showposts=3"); ?>
<?php if(have_posts()): while(have_posts()): the_post(); ?>
<p>●<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<?php endwhile; endif; ?>
<? wp_reset_query(); ?>
query_posts を使用したら必ず <? wp_reset_query(); ?> でループを終わらせる。
でないといくらでもループ表示されてしまうので注意!
if文で条件分岐できる
<!-- アイキャッチ画像設置 -->
<?php if (has_post_thumbnail()): ?>
<?php the_post_thumbnail(); ?>
<?php else: ?>
<?php echo '<img src="<?php echo get_template_directory_uri(); ?>/img/no-image.jpg" alt="noimage" />'; ?>
<?php endif; ?>
カスタムフィールドを使う。
表示ない場合には、右上の「表示オプション」から「カスタムフィールド」を選択する。
functions.php に必要事項を追記
/* カスタムフィールドで外部CSSを複数読み込み */
function include_custom_css(){
if(is_single()||is_page()){
if($css = get_post_meta(get_the_ID(), 'includeCSS', true)){
echo "\n";
}
}
}
add_action('wp_head','include_custom_css');
これでカスタムフィールドにCSSを適応させることが出来る
function register_style() {
wp_register_style('style', get_bloginfo('template_directory').'/style.css');
wp_register_style('home', get_bloginfo('template_directory').'/css/home.css');
wp_register_style('single', get_bloginfo('template_directory').'/css/single.css');
wp_register_style('category', get_bloginfo('template_directory').'/css/category.css');
wp_register_style('archive-information', get_bloginfo('template_directory').'/css/archive-information.css');
wp_register_style('page', get_bloginfo('template_directory').'/css/page.css');
}
function add_stylesheet() {
register_style();
// 全ページ共通
wp_enqueue_style('style');
// TOPページ専用
if (is_home()){
wp_enqueue_style('home');
}
// 投稿・カスタム投稿ページ専用
elseif (is_single()) {
wp_enqueue_style('single');
}
// カテゴリページ専用
elseif (is_category()) {
wp_enqueue_style('category');
}
// カスタム投稿アーカイブページ専用
elseif (is_post_type_archive('ポストタイプ名')) {
wp_enqueue_style('archive-information');
}
// 固定ページ専用
elseif (is_page()) {
wp_enqueue_style('single');
}
}
add_action('wp_print_styles', 'add_stylesheet');
特定の固定ページ
// 固定ページ専用
elseif (is_page(array(1,3))) {
wp_enqueue_style('single');
}
固定ページID 1と3だけに適応 (array(1,3))
<?php if (wp_is_mobile()) :?><!--スマホ用-->
スマートフォン用コンテンツをココに書きます。
<?php else: ?><!--PC用-->
パソコン用コンテンツをココに書きます。
<?php endif; ?>