wordpress的桌面应用端
wordpress提供了桌面应用,支持Windows、Mac以及Linux操作系统。这些应用程序允许用户管理他们的网站、撰写和发布内容,以及执行其他wordpr...
最近,我开始为我工作的公司开发wordPress插件,并重新发现了强大的WordPress。好吧,这么多年后,我花了相当多的时间才再次适应WordPress,但我发现她比我离开她的时候更加成熟和美丽。因此,在本文中,我不打算讨论我在她身上发现的所有新东西,而是讨论一些非常具体的有用功能,大多数开发人员可能不知道或至少忘记使用。那么让我们开始吧——
$posts = get_posts(); $postTitles = wp_list_pluck($posts, 'post_title'); print_r($postTitles); // Output: Array ( [0] => Runolfsdottir-Reynolds [1] => Luettgen and Hoeger [2] => Smith and Kulas )
此函数从数组/对象数组中提取给定的键/属性。此外,它还接受第二个参数,其中键/属性的名称将是返回的关联数组的键。例如:
$posts = get_posts(); $postTitles = wp_list_pluck($posts, 'post_title', 'ID'); print_r($postTitles); // Output: Array ( 10 => Runolfsdottir-Reynolds 11 => Luettgen and Hoeger 12 => Smith and Kulas )
在本例中,10、11 和 12 是每个帖子的 ID。
$posts = get_posts(); $filtered = wp_list_filter($posts, [ 'ID' => 10 ]); print_r($filtered); // Output: Array ( [0] => WP_Post Object ( [ID] => 10 [post_author] => 1 [post_date] => 2019-03-05 21:16:01 [post_date_gmt] => 0000-00-00 00:00:00 [post_content] => Hi There! // ... ) )
此函数根据一组键 => 值对过滤数组/对象数组。在这种情况下,我们可以提供多个键 => 值对,例如:
$posts = get_posts(); $filtered = wp_list_filter($posts, [ 'post_author' => 1, 'comment_status' => 'open' ]);
在这种情况下,结果将是一个post_author为 1 且 comment_status 为 open 的帖子数组。这个函数还接受第三个参数,我们可以在其中指定一个条件和/或何时将有多个 kay => 值对进行过滤。如果给出 和 ,则所有键 => 值对都将匹配,默认情况下,如果任何键 => 值对匹配,则为 and、while 或将拉出帖子,例如:
$people = [ [ 'name' => 'John Doe', 'occupation' => 'Software Developer' ], [ 'name' => 'Jane Doe', 'occupation' => 'Designer' ], [ 'name' => 'Johnny Doe', 'occupation' => 'Tester' ] ]; $filtered = wp_list_filter($people, [ 'name' => 'Johnny Doe', 'occupation' => 'Software Developer' ], 'or'); print_r($filtered); // Output: Array ( [0] => Array ( [name] => John Doe [occupation] => Software Developer ) [2] => Array ( [name] => Johnny Doe [occupation] => Tester ) )
$posts = get_posts(); $sorted = wp_list_sort($posts, 'post_title');
此函数将数组/对象数组作为第一个参数,并按作为第二个参数给出的键名(排序依据)对它们进行排序。默认情况下,它使用升序 (asc) 顺序对列表进行排序,除非我们传递第三个参数来告诉它如何排序,例如:
$posts = get_posts(); $sorted = wp_list_sort($posts, 'post_title', 'desc');
或者,我们可以传递第四个参数来告诉它保留索引/键,例如:
$people = [ [ 'name' => 'John Doe', 'occupation' => 'Software Developer' ], [ 'name' => 'Jane Doe', 'occupation' => 'Designer' ], [ 'name' => 'Johnny Doe', 'occupation' => 'Tester' ] ]; $sorted = wp_list_sort($people, 'occupation', 'desc', true); print_r($sorted); // Output: Array ( [2] => Array ( [name] => Johnny Doe [occupation] => Tester ) [0] => Array ( [name] => John Doe [occupation] => Software Developer ) [1] => Array ( [name] => Jane Doe [occupation] => Designer ) )
请注意,数组使用降序按职业排序,但保留了原始索引/键,第一个数组项的键是 2 而不是 0。 顺便说一句,这个函数还有另一种变体,如下所示:
$people = [...]; $sorted = wp_list_sort($people, [ 'occupation' => 'asc', // order by occupation using asc 'name' => 'desc' // order by name using desc ], null, true);
在本例中,第二个参数(order by)是一个数组,其中传递了一个多个 order by key => 值(order by key => order 类型)的数组,而不是作为字符串传递的单个键名。在这种情况下,如果我们要传递第四个参数以保留索引,则第三个参数必须为 null。
$list = wp_parse_list('One, Two, Three 4 50'); print_r($list); // Output: Array ( [0] => One [1] => Two [2] => Three [3] => 4 [4] => 50 )
此函数实际上从逗号或空格分隔的标量值列表字符串创建一个数组。
$list = wp_parse_id_list([1, 2, '3', '2']); print_r($list); // Output: Array ( [0] => 1 [1] => 2 [2] => 3 )
此函数采用字符串或整数形式的数值数组或字符串,并创建具有完全唯一值的整数数组。下面的示例从以逗号或空格分隔的标量值字符串创建唯一的整数数组:
$list = wp_parse_id_list('1, 2, 3 2 '); print_r($list); // Output: Array ( [0] => 1 [1] => 2 [2] => 3 )
$list = wp_parse_slug_list(['slug1', ' slug2', 'slug 1 ', 'slug2']); print_r($list); // Output: Array ( [0] => slug1 [1] => slug2 [2] => slug-1 )
这个函数就像wp_parse_id_list一样,唯一的区别是,它从数组或逗号或空格分隔的标量值列表中解析唯一的 slugs(作为字符串),例如(从字符串解析):
$list = wp_parse_slug_list('slug1 slug2, slug 1 slug2'); print_r($list); // Output: Array ( [0] => slug1 [1] => slug2 [2] => slug [3] => 1 // also a string )
$people = [ [ 'name' => 'John Doe', 'email' => 'johndoe@gmail.com', 'occupation' => 'Software Developer' ], [ 'name' => 'Jane Doe', 'email' => 'janedoe@gmail.com', 'occupation' => 'Designer' ], [ 'name' => 'Johnny Doe', 'email' => 'johnnydoe@gmail.com', 'occupation' => 'Tester' ] ]; $slice = []; foreach ($people as $person) { $slice[] = wp_array_slice_assoc($person, ['name', 'email']); } print_r($slice); // Output: Array ( [0] => Array ( [name] => John Doe [email] => ok@gmail.com ) [1] => Array ( [name] => Jane Doe [email] => ok@gmail.com ) [2] => Array ( [name] => Johnny Doe [email] => ok@gmail.com ) )
此函数创建关联数组的切片。第一个参数是源数组,我们要从中创建一个切片。第二个参数是另一个非关联数组,它采用源数组的一个或多个键名来创建切片。
$posts = get_posts(); $filtered = wp_filter_object_list( $posts, ['post_date' => '2019-05-05 03:15:32', 'comment_status' => 'open'], 'or', // match any field 'post_title' // only return post_title ); print_r($filtered); // Output: Array ( [0] => Title of Post-1 [1] => Title of Post-2 )
此函数筛选对象数组。第一个参数是对象数组,在本例中给出了一个帖子数组,第二个参数是键 => 个值对的关联数组进行筛选。第三个参数是可选的,它告诉是否匹配对象中的所有键 => 值对来过滤它,我们使用了或匹配任何键 => 值,默认情况下它是 and。最后,第四个可选参数告诉要从过滤/匹配的对象返回哪个字段,除非我们想要整个对象。因此,例如,我们可以按如下方式使用它:
$posts = get_posts(); $filtered = wp_filter_object_list( $posts, ['post_date' => '2019-05-05 03:15:32', 'comment_status' => 'open'] );
在这种情况下,该函数将遍历所有帖子,并过滤掉post_date为 2019-05-05 03:15:32 的帖子,并且comment_status已打开,最后它将返回所有匹配对象的数组。
$posts = get_posts(); $urls = wp_extract_urls($posts[0]->post_content); print_r($urls); // Output: Array ( [0] => https://wpblog.test/wp-content/uploads/2019/01/Afraz.jpg [1] => https://wpblog.test/wp-content/uploads/2019/01/Afreen.jpg )
函数名称本身说明了它的用例。它只是使用正则表达式从任意内容(而不仅仅是从post_content)中提取所有 URL。
if (wp_is_numeric_array(['a', 'b'])) { // It's a numeric-indexed array. }
此函数是自我描述的。它只是确定变量是否为数字索引数组。区分关联数组和数字索引数组的简单方法。
你可能喜欢
wordpress提供了桌面应用,支持Windows、Mac以及Linux操作系统。这些应用程序允许用户管理他们的网站、撰写和发布内容,以及执行其他wordpr...
最近,我们被问及LiteSpeed Cache与WP Rocket哪个更好。 现在,如果您不知道,LiteSpeed Cache和WP Rocket是两个最受欢...
默认情况下,wordpress会在您网站顶部向所有登录用户(如订阅者、作者、编辑者和任何其他用户角色)显示一个管理栏。 此管理栏可以将他们引导至wordpres...