[wp]テーマ自作時によく使うタグメモ

wp以外を触ることが多くなって、せっかく覚えたのがすっぽ抜けてる

包括的なアレ

サイト名

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

キャッチフレーズ

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

WEBサイトURL(末尾/あり)

<?php echo esc_url( home_url( '/' ) ); ?>

テーマディレクトリ(※親テーマ)

<?php echo get_template_directory_uri(); ?>
//style.css読み込み
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/style.css">

ヘッダー・フッター読み込み

<?php get_header(); ?>
<?php get_footer(); ?>

//header-aaa.php読み込み
<?php get_header('aaa'); ?>
//footer-bbb.php読み込み
<?php get_footer('bbb'); ?>

functions.php

アイキャッチ有効化

function twpp_setup_theme() {
    add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( 600, 400, true );
}
add_action( 'after_setup_theme', 'twpp_setup_theme' );

サムネイルサイズ指定

add_image_size('post_120x120_thumbnail', 120, 120, true);
add_image_size('post_150x150_thumbnail', 150, 150, true);

アップロード後の指定は反映されない。再アップロードするか、それ用のプラグインを使って対処する。

親スラッグ指定有効化(.cssとかをifで振り分けるとき用)

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読み込み

<?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読み込み

<?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; ?>

これに限らず条件は色々指定できる

ナビゲーション宣言

function menu_setup() {  
    register_nav_menus( array(
        'global' => 'グローバルメニュー',
        'footer' => 'フッターメニュー',
    ));
}
add_action( 'after_setup_theme', 'menu_setup' );

title生成(どういう作用なのかは説明してる記事を読む)

add_theme_support( 'title-tag' );

投稿周り

記事タイトル

<?php the_title(); ?>

記事本文

<?php the_content(); ?>

記事抜粋

<?php the_excerpt(); ?>

パーマリンク

<?php the_permalink(); ?>

投稿日時

<?php the_time(); ?>
<time class="published" datetime="<?php the_time('c'); ?>"><?php the_time(get_option('date_format')); ?></time>

カテゴリ・タグ

<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>

編集ボタン(ログイン時のみ)

<?php edit_post_link('この記事を編集', '<span class="edit">', '</span>'); ?>

サムネイル

<?php if(has_post_thumbnail()): ?>
    <?php the_post_thumbnail(); ?>
<?php endif; ?>

前の記事・次の記事(single.php)

<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読み込み

//sidebar.php
<?php get_sidebar(); ?>
//sidebar-blog.php
<?php get_sidebar('blog'); ?>

カスタム投稿周り

カスタム投稿一覧+ページャー(wp_pagenavi)

    <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>

投稿数カウント・活用

<?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; ?>

応用:ターム指定したカスタム投稿一覧

<?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'
        	)
		)
    )); 
?>

もっと細かく指定したい場合はこちらを確認

コメント

タイトルとURLをコピーしました