Wordpress 1

6. fucntions.php

追加する項目

  • ページのタイトルを出力
    add_theme_support( 'title-tag' );
    
    function wp_document_title_separator( $separator ) {
    	$separator = '|';
    	return $separator;
    }
    add_filter( 'document_title_separator', 'wp_document_title_separator' );
    
  • カスタムメニューを有効化
    function register_menus() { 
      register_nav_menus( array(
        'main-menu' => 'Main Menu',
      ) );
    }
    add_action( 'after_setup_theme', 'register_menus' );
    
    //カスタムメニューを出力(テンプレート側)
    <?php wp_nav_menu( array( 'theme_location' => 'header-navi' ) ); ?>
    
  • アイキャッチ画像を有効化
    add_theme_support( 'post-thumbnails' );
    
    //アイキャッチ画像を表示
    <?php the_post_thumbnail('thumbnail'); ?>
    ※thumbnail、medium、largeをパラメーターに設定可能
    
    //幅と高さを指定することも可能
    <?php the_post_thumbnail(array(100, 100)); ?>
    

7. AdvancedCustomFields

基本表示

基本的な使い方
<?php if( get_field('sub_heading') ): ?>
	<h2><?php the_field('sub_heading'); ?></h2>
<?php endif; ?>

画像の表示1(戻り値をURLにする)
<?php if(get_field('sub_heading')): ?>
	<h2><?php the_field('sub_heading'); ?></h2>
<?php endif; ?>

画像の表示2(戻り値を画像配列にする)
<?php
 $image = get_field('フィールド名');
 $src = $image['sizes']['thumbnail'];//large,mediumなど
 $width = $image['sizes']['thumbnail-width'];
 $height = $image['sizes']['thumbnail-height'];
?>
<img src="<?php echo $src; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" alt="">