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

Takes of the function's arguments about how their own

The argument received or if you want to create custom functions, as query_posts,

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

If you want to format is a way of like.

use of wp_parse_args
  (array argument, the default) wp_parse_args 

Go to an associative array 'key = value' to split it on '&' arguments when you use it.
For those with no value will use the initial value.
Specifically used as follows.

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に戻る