WordPressに関する情報や技術紹介です

Prende degli argomenti della funzione di come il proprio

L'argomento ricevuto o se si desidera creare funzioni personalizzate, come query_posts,

  query_posts ('cat = 5 & order = DESC') 

Se si desidera formattare è un modo di simile.

uso di wp_parse_args
  (Argomento array, l'impostazione predefinita) wp_parse_args 

Vai a 'valore della chiave =' un array associativo per dividere su '&' gli argomenti quando lo si utilizza.
Per chi ha alcun valore utilizzerà il valore iniziale.
Specificamente utilizzato come segue.

function myfunction( $args='' ) {

	// 初期値の設定
	$defaults = array('key1' => 10, 'key2' => 0, 'order' => 'DESC', 'key3' => 'value');

	// 引数を取得
	$args = wp_parse_args($args, $defaults);

	if($args['key2'] > 0) {
		// 処理
	}
}

関数を使う場合は以下のようになります。

// 関数呼び出し
myfunction('key2=10&key3=test');

これで後々に引数を追加しても順番を気にする必要が無いので拡張し易くなります。

タグ
, , , ,
トラックバックURL

Leave a Reply

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

ページTOPに戻る