wp_enqueue_style() テンプレートタグ WordPress関数

wp_enqueue_style()

参照:WordPress Codex-wp_enqueue_style

構文

wp_enqueue_style( ①$handle[必須], ②$src[オプション], ③$deps[オプション], ④$ver[オプション], ⑤$media[オプション] )

 

wp_enqueue_style()の詳細

 動 作 

CSSスタイルシートのファイルを安全にキュー(queue)へ追加する。

 

 返り値 

返り値:(void)なし

 

 引 数 

①$handle[必須]

(文字列)(必須)

ハンドル名:システム上の名前

 

②$src[オプション]

スタイルシートのURL。デフォルトはfalse

 

③$deps[オプション]

依存関係:このCSSよろ前に呼び出すCSS。デフォルトは空の配列。

 

④$ver[オプション]

バージョン:CSSのバージョン。デフォルトはfalse。

 

⑤$media[オプション]

メディア:CSSに付けるメディアタイプ。デフォルトはall。

 

wp_enqueue_style()のコード

tags/5.3/src/wp-includes/functions.wp-styles.php

<?php


/**
 * Initialize $wp_styles if it has not been set.
 *
 * @global WP_Styles $wp_styles
 *
 * @since 4.2.0
 *
 * @return WP_Styles WP_Styles instance.
 */
function wp_styles() {
	global $wp_styles;
	if ( ! ( $wp_styles instanceof WP_Styles ) ) {
		$wp_styles = new WP_Styles();
	}
	return $wp_styles;
}


/**
 * Enqueue a CSS stylesheet.
 *
 * Registers the style if source provided (does NOT overwrite) and enqueues.
 *
 * @see WP_Dependencies::add()
 * @see WP_Dependencies::enqueue()
 * @link https://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
 *
 * @since 2.6.0
 *
 * @param string           $handle Name of the stylesheet. Should be unique.
 * @param string           $src    Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
 *                                 Default empty.
 * @param string[]         $deps   Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
 * @param string|bool|null $ver    Optional. String specifying stylesheet version number, if it has one, which is added to the URL
 *                                 as a query string for cache busting purposes. If version is set to false, a version
 *                                 number is automatically added equal to current installed WordPress version.
 *                                 If set to null, no version is added.
 * @param string           $media  Optional. The media for which this stylesheet has been defined.
 *                                 Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
 *                                 '(orientation: portrait)' and '(max-width: 640px)'.
 */
function wp_enqueue_style( $handle, $src = '', $deps = array(), $ver = false, $media = 'all' ) {
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );

	$wp_styles = wp_styles();

	if ( $src ) {
		$_handle = explode( '?', $handle );
		$wp_styles->add( $_handle[0], $src, $deps, $ver, $media );
	}
	$wp_styles->enqueue( $handle );
}


//class.wp-dependencies.phpより

/**
	 * Queue an item or items.
	 *
	 * Decodes handles and arguments, then queues handles and stores
	 * arguments in the class property $args. For example in extending
	 * classes, $args is appended to the item url as a query string.
	 * Note $args is NOT the $args property of items in the $registered array.
	 *
	 * @since 2.1.0
	 * @since 2.6.0 Moved from `WP_Scripts`.
	 *
	 * @param string|string[] $handles Item handle (string) or item handles (array of strings).
	 */
	public function enqueue( $handles ) {
		foreach ( (array) $handles as $handle ) {
			$handle = explode( '?', $handle );

			if ( ! in_array( $handle[0], $this->queue, true ) && isset( $this->registered[ $handle[0] ] ) ) {
				$this->queue[] = $handle[0];

				// Reset all dependencies so they must be recalculated in recurse_deps().
				$this->all_queued_deps = null;

				if ( isset( $handle[1] ) ) {
					$this->args[ $handle[0] ] = $handle[1];
				}
			}
		}
	}


?>

 

wp_enqueue_style()を使ったコード例

 実装 

<!--functions.phpの記述-->

<?php
wp_enqueue_style('example-style',get_stylesheet_uri());
?>

<!--html出力-->

<link 
rel='stylesheet' 
id='example-style-css' 
href='https://nawate.jp/wp-content/themes/nawate/style.css?ver=4.4.4' 
type='text/css' 
media='all' 
/>

WordPressのおすすめ-カスタマイズ編

おすすめ

 

スポンサードリンク

スポンサードリンク