我创建了一个名为$YPE_slugs_names
的新数组来存储标签slug和标签get_the_tags();
函数中的名称,其中标签slug设置为$YPE_slugs_names
键,标签名称设置为{{1值。我在下面尝试了这段代码,但没有和我一起工作(我在循环中使用了这段代码,因为不需要帖子ID)
$YPE_slugs_names
答案 0 :(得分:2)
如果我理解你的问题,你应该想要这个。
<?php
$YPE_slugs_names = array();
$YPE_tags = get_the_tags();
if ($YPE_tags) {
foreach($YPE_tags as $YPE_tag) {
$YPE_slugs_names[$YPE_tag->slug] = $YPE_tag->name;
}
}
?>
根据https://developer.wordpress.org/reference/functions/get_the_tags/,get_the_tags()
会返回如下内容:
/*
This above prints the tag objects for post ID #24 (if post has any tags):
Array
(
[0] => WP_Term Object
(
[term_id] => 108
[name] => tag-1
[slug] => tag-1
[term_group] => 0
[term_taxonomy_id] => 109
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[filter] => raw
[object_id] => 24
)
[1] => WP_Term Object
(
[term_id] => 109
[name] => tag-2
[slug] => tag-2
[term_group] => 0
[term_taxonomy_id] => 110
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 1
[filter] => raw
[object_id] => 24
)
)
*/
所以我的代码将生成一个这样的数组(name as value,slug as key):
Array
(
[tag-1] => tag-1,
[tag-2] => tag-2
)