functions.phpを設定する

2020年1月14日 (更新日:2020年4月15日)

functions.phpを設定する:functions.phpを設定する ペ...


functions.phpを設定する

ページタイトルを表示させる為のphpコードを記述しましたがこのままでは表示しません。functions.phpの中にタイトルを表示させる為のphpコードを書いておかなければなりません。
新規ページを作成してファイル名をfunctions.phpにして保存。まずは

 

<?php
if ( ! function_exists( 'lab_setup' ) ) :

  function lab_setup() {



}
endif;
add_action( 'after_setup_theme', 'lab_setup' );
?>

を記述します。

 

カスタムヘッダー

function lab_setup() {

}
の中に下記を記述してタイトルを表示させます。

/*
   * カスタムヘッダー
   */

  $defaults = array(
    'default-image' => '',
    'random-default' => false,
    'width' => 2000,
    'height' => 700,
    'flex-height' => false,
    'flex-width' => false,
    'default-text-color' => '',
    'header-text' => true,
    'uploads' => true,
    'wp-head-callback' => '',
    'admin-head-callback' => '',
    'admin-preview-callback' => '',
    'video' => true,
    'video-active-callback' => 'is_front_page'
  );
  add_theme_support( 'custom-header', $defaults );

値はケースバイケースで設定してみて下さい。
保存したらリロードします。するとタイトルが表示されました。
管理画面も確認してみます。「外観」の中の「カスタマイズ」の中の「サイトの基本情報」の中に入るとサイトのタイトルとキャッチフレーズが入力できます。その直下に「サイトのタイトルとキャッチフレーズを表示」にチェックが入り切りできます。チェックを入れるとタイトルが表示され、チェックを外すと非表示になります。
次にWordPressではタイトルを画像として設置する事ができます。

index.php中のh1タグの中身の

<?php
if( display_header_text() ) {
の上に

<?php the_custom_logo(); ?>

を記述します。

<h1>
      <?php the_custom_logo(); ?>
      <?php
      if( display_header_text() ) {
        ?>
        <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a>
        <?php
      } ?>
</h1>

 

カスタムロゴ

そしてfunctions.phpに

/*
   * カスタムロゴ
   */

    add_theme_support( 'custom-logo', array(
      'height'      => auto,
      'width'       => auto,
      'flex-height' => true,
      'flex-width'  => true,
      'header-text' => array( 'site-title', 'site-description' ),
    ) );

 

を追加します。
管理画面の中のサイトの基本情報の中にロゴを選択できる項目が追加されます。選択してみます。すると画像がタイトルとして設置されます。ただサイトのタイトルとしてテキストも表示されてますので「サイトのタイトルとキャッチフレーズを表示」のチェックを外します。すると画像だけになりました。

最終的なfunctions.phpの中身は

<?php
if ( ! function_exists( 'lab_setup' ) ) :

  function lab_setup() {

  /*
   * カスタムヘッダー
   */

  $defaults = array(
    'default-image' => '',
    'random-default' => false,
    'width' => 2000,
    'height' => 700,
    'flex-height' => false,
    'flex-width' => false,
    'default-text-color' => '',
    'header-text' => true,
    'uploads' => true,
    'wp-head-callback' => '',
    'admin-head-callback' => '',
    'admin-preview-callback' => '',
    'video' => true,

    'video-active-callback' => 'is_front_page'
  );
  add_theme_support( 'custom-header', $defaults );

  /*
   * カスタムロゴ
   */

  add_theme_support( 'custom-logo', array(
    'height'      => auto,
    'width'       => auto,
    'flex-height' => true,
    'flex-width'  => true,
    'header-text' => array( 'site-title', 'site-description' ),
  ) );



    }
  endif;
  add_action( 'after_setup_theme', 'lab_setup' );
  ?>

になります。