arg在WordPress中意味着什么

时间:2016-12-16 08:16:46

标签: php arrays wordpress

最近我一直在使用API​​,在那里我发现了这个名为$ args_something的数组。我以为这只是一些随机的名字。几天之后,我看到一个完全不同的代码,同样类型的数组也以$ args开头。这是什么意思。这两个代码都是用PHP编写的。

阵列结构的一个例子:

$args = array( 

//////Author Parameters - Show posts associated with certain author.
    //http://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
    'author' => '1,2,3,',                     //(int) - use author id [use minus (-) to exclude authors by ID ex. 'author' => '-1,-2,-3,']
    'author_name' => 'luetkemj',              //(string) - use 'user_nicename' (NOT name)
    'author__in' => array( 2, 6 ),            //(array) - use author id (available with Version 3.7).
    'author__not_in' => array( 2, 6 ),        //(a

我必须说两个代码最初来自WordPress。

1 个答案:

答案 0 :(得分:2)

  

这只是增加代码可读性的命名约定。您可以使用任何不会影响代码的名称替换它。

示例I:

$args_user = array(
    'number' => 10
);
$user_query = new WP_User_Query($args_user);
$users = $user_query->get_results();

//------------------------------------------

$args_post = array(
    'numberposts' => -1,
    'post_type' => 'post'
);
$posts = get_posts($args_post);

示例II:

$my_args_1 = array(
    'number' => 10
);
$query_1 = new WP_User_Query($my_args_1);
$data = $query_1->get_results();

//------------------------------------------

$my_args_2 = array(
    'numberposts' => -1,
    'post_type' => 'post'
);
$data = get_posts($my_args_2);

代码示例I和II都具有相同的输出,但是第一个示例具有良好的用户可读性,如果您有一个很长的方法/代码,那么在看到变量名后您就可以轻松识别。

相关问题